Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change properties of buttons within button boxes in Qt Designer?

Tags:

c++

qt

qt4

designer

I have been searching online to no avail. Does anyone know how to access a button in a button box (created using the "Dialog with Buttons Right" template)?

like image 731
Dark Star1 Avatar asked Apr 19 '10 22:04

Dark Star1


2 Answers

In Designer, select the OK or Cancel button. Then open the property editor and scroll down to the QDialogButtonBox section. You can then expand the standardButtons item to see the various buttons that are available. Other properties, such as the centerButtons property, are also available.

However, designer gives you very little control over the button box.

In code, you can do many other things, such as change the text that appears on the "standard buttons." From the documentation:

findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);

moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
moreButton->setAutoDefault(false);

buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);

As long as you give the button box a name in designer, you can set these properties in code.

like image 60
Kaleb Pederson Avatar answered Sep 22 '22 18:09

Kaleb Pederson


I am writing this answer for the Python community. I am using PySide and faced a similar problem. I have a QDialogButtonBox and I would like to have my own buttons instead of the default ones.

I am using PySide which is more or less the exact replica of the c++ code, so I believe other c++ developers can also get something from it.

Here how I would do that:

        my_ok_button = QtGui.QPushButton("My Ok Button")
        my_cancel_button = QtGui.QPushButton("My Cancel Button")
        ok_cancel_button = QtGui.QDialogButtonBox(QtCore.Qt.Horizontal)
        ok_cancel_button.addButton(my_ok_button, QtGui.QDialogButtonBox.ButtonRole.AcceptRole)
        ok_cancel_button.addButton(my_cancel_button, QtGui.QDialogButtonBox.ButtonRole.RejectRole)

I would then insert my button box to my layout like ususal:

layout.addWidget(ok_cancel_button, 1, 1)

Now later in my code I can do anything with my button. Lets change its name:

my_ok_button.setText("Some Other Name")

So then things to note here is that:

  • you must set the role of the buttons in the addButton() method if you want to use functionalities given by standard buttons. E.g. if you
    wish to do something like below, you need to have the button roles set.

    ok_cancel_button.accepted.connect(self.ok_method_handler) ok_cancel_button.rejected.connect(self.close)

More information can be found here.

like image 35
Rash Avatar answered Sep 19 '22 18:09

Rash