1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

There's no buttonClicked signal, only clicked.
Apologies, I was thinking of QMessageBox - you are right that QDialogButtonBox uses 'clicked' instead. I will edit my post to reflect, but I think the same solution applies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.