Yes / No message box using QMessageBox

How to show a message box with Yes / No buttons in Qt and how to check which of them were pressed?

those. message box that looks like this:

enter image description here

+76
c ++ qt qmessagebox
Oct 28 '12 at 18:24
source share
5 answers

You would use QMessageBox::question .

An example in a hypothetical widget slot:

 #include <QApplication> #include <QMessageBox> #include <QDebug> // ... void MyWidget::someSlot() { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "Test", "Quit?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { qDebug() << "Yes was clicked"; QApplication::quit(); } else { qDebug() << "Yes was *not* clicked"; } } 

Should work in Qt 4 and 5, QT += widgets on Qt 5 and CONFIG += console on Win32 are qDebug() to see qDebug() output.

Go to the StandardButton list to get a list of buttons that you can use; the function returns the button that the button was pressed on. You can set the default button with an additional argument (Qt "automatically selects the appropriate default value" if you did not specify or specify QMessageBox::NoButton ).

+133
Oct 28 '12 at 18:35
source share

You can use the QMessage object to create a message box, and then add buttons:

 QMessageBox msgBox; msgBox.setWindowTitle("title"); msgBox.setText("Question"); msgBox.setStandardButtons(QMessageBox::Yes); msgBox.addButton(QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); if(msgBox.exec() == QMessageBox::Yes){ // do something }else { // do something else } 
+27
Oct 28 '12 at 18:32
source share

QT can be as simple as Windows. Equivalent code

 if (QMessageBox::Yes == QMessageBox(QMessageBox::Information, "title", "Question", QMessageBox::Yes|QMessageBox::No).exec()) { } 
+15
Nov 02 '13 at 19:22
source share

I am missing the tr translation in the answers.

One of the simplest solutions that allows later internationalization:

 if (QMessageBox::Yes == QMessageBox::question(this, tr("title"), tr("Message/Question"))) { // do stuff } 

It's usually a good Qt habit to place code level lines inside a tr("Your String") call.

( QMessagebox , as mentioned above, works as part of any QWidget method)

EDIT:

you can use the QMesssageBox outside the QWidget context, see @TobySpeight answer.

If you are even outside the context of QObject , replace tr with qApp->translate("context", "String") - you need #include <QApplication>

+2
Mar 14 '17 at 16:57
source share

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 ; } 
+2
Mar 14 '17 at 17:44
source share



All Articles