Search code examples
qt

QT QMessageBox default button?


I've got a QMessageBox with Yes and No alternatives.

QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "New update", "There is a new update, do you want to update now?", QMessageBox::Yes|QMessageBox::No);

if (reply == QMessageBox::Yes) {

} else {

}

But for some reason the default answer is always No. How can I set the default button to Yes

I've tried:

reply.setDefaultButton(QMessageBox::Yes);

But cannot get that to work.


Solution

  • QMessageBox::question can be passed an extra parameter that has the default value of NoButton, and you are not using that parameter, see the documentation here, so your code will become:

    reply = QMessageBox::question(this, "New update", "There is a new update, do you want to update now?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);