简体   繁体   中英

How to execute a function after click particular button in PyQt5 Dialog Button Box?

Here is my Dialog Button Box design on Python:

    self.buttonBox = QtWidgets.QDialogButtonBox(Frame)
    self.buttonBox.setGeometry(QtCore.QRect(200, 216, 144, 27))
    self.buttonBox.setFont(font)
    self.buttonBox.setAcceptDrops(False)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Discard|QtWidgets.QDialogButtonBox.Save)
    self.buttonBox.setObjectName("buttonBox")

There are two buttons as shown above, which are Discard and Save . How to execute a particular unique function when I click Save or Discard . What I have tried is:

    self.buttonBox.accepted.connect(self.save)
    self.buttonBox.rejected.connect(self.discard)

However, the function execute only when I clicked Save and did not work when I clicked Discard . How to handle this?

Discard is a destructive button, not a reject button. You could either:

  • Use Cancel , Close , or Abort and link to the rejected signal as you are already doing
  • If you need to use Discard , connect to the clicked signal and just verify in your self.discard function that the clicked button (passed with the signal) was the Discard button before doing anything

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM