25

I have added a QDialogButtonBox button with the default Cancel and OK buttons.

Is there a way to change the caption of these buttons? For example, OK should become Run.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
matteo
  • 4,683
  • 9
  • 41
  • 77

1 Answers1

40

You will have to do some coding in your cpp file:

ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Run");
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("Exit");

Note that you may also need to include the QPushButton header:

#include <QPushButton>

Update:

Did not notice the pyqt tag. I'm not familiar with Python (and PyQt in particular), but I think this should do the job:

self.ui.buttonBox.button(QDialogButtonBox.Ok).setText("Run")
self.ui.buttonBox.button(QDialogButtonBox.Cancel).setText("Cancel")

Also, as pointed out by @Kuba Ober, changing the text of standard buttons is not the best approach. The most correct way is to add custom buttons with an appropriate role.

self.ui.buttonBox.addButton("Run", QDialogButtonBox.ActionRole)
kefir500
  • 4,184
  • 6
  • 42
  • 48
  • So using Qt Designer it is not possible to do this modification.. just by adding the code in the `.py` – matteo Jul 08 '15 at 13:25
  • @matteo I'm afraid it's not possible to do this via Qt Designer. All in all, writing the custom code is not that hard. – kefir500 Jul 08 '15 at 13:28
  • @kefir500, yeah that's sound pretty easy.. however I'm a bit surprised that a simple task like this has not been integrated in Qt.. Anyway.. thanks! – matteo Jul 08 '15 at 13:34
  • @kefir500 the `.setText` option is nolong working in PyQt5. It throwsback a exec_() == false or self.reject() resulting in -> exit app. – ZF007 Dec 05 '17 at 13:05
  • 2
    @ZF007. The `setText` method works perfectly fine in PyQt5 - there must be other problems with your code. – ekhumoro Dec 05 '17 at 18:36
  • @ekhumoro .. yup...import QDialogButtonbox or `QDialogButtonBox`.. almost Shakespear! – ZF007 Dec 06 '17 at 10:57
  • had to remove `.ui` from the lines to work. `self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setText("Fine!")` – Ghasem Jan 14 '18 at 16:53
  • Once I add the new button with `self.buttonBox.addButton("Run", QDialogButtonBox.ActionRole)` how do I remove the "OK" button? – Echeban Dec 16 '19 at 22:41
  • Sorry, I answered my own comment: In the UI built by QtDesigner I found `self.buttonBox_calculate.setStandardButtons(QtWidgets.QDialogButtonBox.Close|QtWidgets.QDialogButtonBox.Ok)` Just before adding the "Run" button, I added this line to my code `self.buttonBox_calculate.setStandardButtons(QtWidgets.QDialogButtonBox.Close)` It removes the "OK" button. – Echeban Dec 16 '19 at 22:47