QMessageBox includes static methods to quickly ask questions like this:
#include <QApplication> #include <QMessageBox> int main(int argc, char **argv) { QApplication app{argc, argv}; while (QMessageBox::question(nullptr, qApp->translate("my_app", "Test"), qApp->translate("my_app", "Are you sure you want to quit?"), QMessageBox::Yes|QMessageBox::No) != QMessageBox::Yes) // ask again ; }
If your needs are more complex than those provided by static methods, you should create a new QMessageBox object and call its exec() method to show it in your own event loop and get the identifier of the button pressed. For example, we could make No a default answer:
#include <QApplication> #include <QMessageBox> int main(int argc, char **argv) { QApplication app{argc, argv}; auto question = new QMessageBox(QMessageBox::Question, qApp->translate("my_app", "Test"), qApp->translate("my_app", "Are you sure you want to quit?"), QMessageBox::Yes|QMessageBox::No, nullptr); question->setDefaultButton(QMessageBox::No); while (question->exec() != QMessageBox::Yes) // ask again ; }
Toby Speight Mar 14 '17 at 17:44 2017-03-14 17:44
source share