CppLearner
CppLearner

Reputation: 17040

How do I customize dialog signal/slot in pyQT?

I have a set of buttons, OK and Cancel

buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok|
                                    QtGui.QDialogButtonBox.Cancel)

I want a dialogue prompt when we click on Cancel

self.connect(buttonBox, SIGNAL("rejected()"),
                            self, SLOT("reject()"))
    def reject(self):
        print 'hello'
        self.emit(SIGNAL("reject()"))

I am not sure what to emit. I don't want to just close the thing. I know how to create a QMessageBox when I press X. I want to do the prompt and closing in reject.

I hope it makes sense. Thanks.


For your information, when I press X to close the entire application, I have an overriden method

def closeEvent(self, event):
    reply = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to quit?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    if reply == QtGui.QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

This override self.close() method.

Upvotes: 0

Views: 2455

Answers (3)

koberone
koberone

Reputation: 27

Let's overwrite the accept() function.

def accept(self):
    if validation_ok():
        super().accept()

Upvotes: 0

cuda12
cuda12

Reputation: 660

You might wanna overwrite the accept() function of your QDialog class.

For example:

def accept(self):
    if your_validation_userconfirmation_fct():
        self.done(QtWidgets.QDialog.Accepted)

Upvotes: 0

Avaris
Avaris

Reputation: 36715

You don't emit anything. QDialog has a reject() slot that sets the return code to Rejected and closes the dialog. You need to call that. You named your custom slot reject as well, thus overriding it. You can still call it like:

super(NameOfClass, self).reject()

or change your slot name to something else and use:

self.reject()

in there.

Upvotes: 1

Related Questions