what determines the order of the pushbuttons in a dialog?
-
I have this code:
QPushButton* closeButton = new QPushButton(tr("&Close")); QPushButton* revertButton = new QPushButton(tr("&Revert")); QPushButton* submitButton = new QPushButton(tr("&Submit")); submitButton->setAutoDefault(false); revertButton->setAutoDefault(false); closeButton->setDefault(true); connect(closeButton, &QPushButton::clicked, this, &PasswordDetailsDlg::close); connect(revertButton, &QPushButton::clicked, this, &PasswordDetailsDlg::revert); connect(submitButton, &QPushButton::clicked, this, &PasswordDetailsDlg::submit); QDialogButtonBox* buttonBox = new QDialogButtonBox; buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole); buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); return buttonBox;
But instead of placing the submit button first, then the revert then the close, it places the revert, then the submit and then the close. What order is that???
How can we control the order of pushbuttons in a dialog button box? Does it have to do with the roles?
-
@jdent said in what determines the order of the pushbuttons in a dialog?:
QDialogButtonBox* buttonBox = new QDialogButtonBox; buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole); buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
The thing is, these roles make life easier, because they automatically
accept
orreject
your dialog when being clicked. But like @JonB said, nobody forces you to use aQDialogButtonBox
in your dialog. It's nice to have, but if you don't like the OS default order (on some OS "OK" is left and "Cancel" right and on some it's reversed), you can put them in a regular layout and manually handle theaccept()
/reject()
states. -
@jdent
Yes. Have you read the description at https://doc.qt.io/qt-6/qdialogbuttonbox.html#details ? The point is it takes over the placement and styling to "conforms to the interface guidelines for that platform". The roles determine where it decides things go.Try all of yours with, say,
QDialogButtonBox::ApplyRole
at the same time, does it then just follow the order you add them?You realize that if you really want to control the layout yourself don't bother to use
QDialogButtonBox
! You don't have to, it's there to help if you want the platform to decide what it looks like. -
@jdent
In general, QWidget::setTabOrder()
You can do this in Qt Designer if you use Designer forms.Edit: did not pick up you were trying to do this in a button box.
-
@jdent
Anything you like,QGroupBox
or not. (Except a dialog would not normally put its buttons in a box, but it does not stop you doing it.) Or just a layout. You are free to put whatever you want anywhere on aQDialog
just like on any other widget. -
@jdent said in what determines the order of the pushbuttons in a dialog?:
QDialogButtonBox* buttonBox = new QDialogButtonBox; buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole); buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
The thing is, these roles make life easier, because they automatically
accept
orreject
your dialog when being clicked. But like @JonB said, nobody forces you to use aQDialogButtonBox
in your dialog. It's nice to have, but if you don't like the OS default order (on some OS "OK" is left and "Cancel" right and on some it's reversed), you can put them in a regular layout and manually handle theaccept()
/reject()
states. -