Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the caption of a button in a QDialogButtonBox?

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.

like image 671
matteo Avatar asked Jul 08 '15 11:07

matteo


1 Answers

You will have to do some slight 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)
like image 51
kefir500 Avatar answered Nov 09 '22 10:11

kefir500