How to add a slot to the button, for example, "OK", when using the Dialog Button Box?

    2 answers 2

    1. You can use the QDialogButtonBox::buttons() method, which will display a list of all the buttons of the dialog, find the necessary one among them and bind the slot to its signal.

    2. You can use the QDialogButtonBox::button(StandardButton) method, which is passed the "type" of the button as an argument. The result of the call will be a pointer to the button.

    3. You can add a button to the button box yourself using one of the overloaded addButton methods:

    4. But the most correct method would be not to use what the developers did not anticipate. If you need to scratch a button from the dialogue, it means that you have a problem with the architecture or with the design. If you need a particular behavior of a particular button, you need to create a separate button. Or use the provided button box interface - its slots:

      • accepted() (called when the button with the role AcceptRole or YesRole );

      • clicked(QAbstractButton*) (called when you press any button of the dialog, the pointer to the button will be passed as a signal parameter);

      • helpRequested() (called when the button with the HelpRole role is HelpRole );

      • rejected() (called when a button with the role of a RejectRole or NoRole ).

      On the example of the 'ok' button:

       self.button_ok = self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok) self.button_ok.clicked.connect(self.open_new_dialog) 

      On the example of the 'retry' button:

       self.button_retry = self.buttonBox.button(QtWidgets.QDialogButtonBox.Retry) self.button_retry.clicked.connect(lambda: self.end_dialog(0))