Yes/No message box using QMessageBox

前端 未结 7 2083
渐次进展
渐次进展 2021-01-30 05:59

How do I show a message box with Yes/No buttons in Qt, and how do I check which of them was pressed?

I.e. a message box that looks like this:

相关标签:
7条回答
  • 2021-01-30 06:30

    QMessageBox includes static methods to quickly ask such questions:

    #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 provided for by the static methods, you should construct a new QMessageBox object, and call its exec() method to show it in its own event loop and obtain the pressed button identifier. For example, we might want to make "No" be the 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
            ;
    }
    
    0 讨论(0)
  • 2021-01-30 06:33

    I'm missing the translation call tr in the answers.

    One of the simplest solutions, which allows for later internationalization:

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

    It is generally a good Qt habit to put code-level Strings within a tr("Your String") call.

    (QMessagebox as above works within any QWidget method)

    EDIT:

    you can use QMesssageBox outside a QWidget context, see @TobySpeight's answer.

    If you're even outside a QObject context, replace tr with qApp->translate("context", "String") - you'll need to #include <QApplication>

    0 讨论(0)
  • 2021-01-30 06:35

    QT can be as simple as that of Windows. The equivalent code is

    if (QMessageBox::Yes == QMessageBox(QMessageBox::Information, "title", "Question", QMessageBox::Yes|QMessageBox::No).exec()) 
    {
    
    }
    
    0 讨论(0)
  • 2021-01-30 06:44

    You would use QMessageBox::question for that.

    Example in a hypothetical widget's 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 on Qt 4 and 5, requires QT += widgets on Qt 5, and CONFIG += console on Win32 to see qDebug() output.

    See the StandardButton enum to get a list of buttons you can use; the function returns the button that was clicked. You can set a default button with an extra argument (Qt "chooses a suitable default automatically" if you don't or specify QMessageBox::NoButton).

    0 讨论(0)
  • 2021-01-30 06:44

    Python equivalent code for a QMessageBox which consist of a question in it and Yes and No button. When Yes Button is clicked it will pop up another message box saying yes is clicked and same for No button also. You can push your own code after if block.

    button_reply = QMessageBox.question(self,"Test", "Are you sure want to quit??", QMessageBox.Yes,QMessageBox.No,)
    
    if button_reply == QMessageBox.Yes:
        QMessageBox.information(self, "Test", "Yes Button Was Clicked")
    else :
        QMessageBox.information(self, "Test", "No Button Was Clicked")
    
    
    0 讨论(0)
  • 2021-01-30 06:47

    You can use the QMessage object to create a Message Box 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
    }
    
    0 讨论(0)
提交回复
热议问题