Execute function after clicking OK (QDialogButtonBox)

I am using python 2.7 with PyQT5, this is my button:

self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(50, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.buttonBox.clicked.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)

etc....

if __name__ == "__main__":

app = QApplication(sys.argv)
window = QDialog()
ui = Ui_Dialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

How can I execute the function after clicking OK?

+4
source share
2 answers

Do not connect to buttonBox.clicked, because it will be called for each button.

Your connections to the key boxes should look like this:

    self.buttonBox.accepted.connect(Dialog.accept)
    self.buttonBox.rejected.connect(Dialog.reject)

To start the function / slot when the dialog is accepted (i.e. only when you click OK), do the following:

    self.accepted.connect(some_function)

If you want to pass a parameter, use lambda:

    self.accepted.connect(lambda: some_function(param))
+7
source

The buttonBox button setting should look like

self.buttonBox.clicked.connect(Dialog.accept)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(Dialog.reject)

where self.acceptis the function defined in the class.

def accept(self):

, , .

+3

Source: https://habr.com/ru/post/1614623/


All Articles