QButtonBox set default button
qt, user-interface
Solution
Make sure you set auto default `false` as well, use `setAutoDefault(false)` as well as `setDefault(false)`.
Example code below.
#include <QtWidgets>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QDialogButtonBox* bb = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QPushButton* okBtn = bb->button(QDialogButtonBox::Ok);
okBtn->setAutoDefault(true);
okBtn->setDefault(true);
QPushButton* caBtn = bb->button(QDialogButtonBox::Cancel);
caBtn->setAutoDefault(false);
caBtn->setDefault(false);
QDialog dlg;
QVBoxLayout* dlgLayout = new QVBoxLayout();
dlgLayout->addWidget(bb);
dlg.setLayout(dlgLayout);
dlg.show();
return app.exec();
}
When I tested this on Windows, the OK button was the default button by default, but I could swap that to the cancel button by changing the calls to `setAutoDefault` and `setDefault`.
Problem
Under Qt 5.3, the default button of a QButtonBox is `Cancel` and I want to set it to `Ok` but I can't find a way to achieve it. I've tried this : ``` QPushButton * b = ui->buttonBox->button(QDialogButtonBox::Ok); b->setDefault(true); ``` but with no success, it throws : ``` /Users/thomas/Dev/Joker/app/Joker/RulerSpaceDialog.cpp:18:3: error: member access into incomplete type 'QPushButton' b->setDefault(true); ^ /Applications/Qt/5.3/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:50:7: note: forward declaration of 'QPushButton' class QPushButton; ^ 1 error generated. ``` I also try by browsing the list but with no luck.... EDIT : I added the include to get that code : ``` QPushButton * b = ui->buttonBox->button(QDialogButtonBox::Ok); if(b) { b->setDefault(true); qDebug() << b->text(); } ``` Which outputs `Ok` waits 2secs then highlight the `Cancel` button...