ChatGPT解决这个技术问题 Extra ChatGPT

Yes/No message box using QMessageBox

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:

https://i.stack.imgur.com/h92PA.png


M
Mat

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).


I have a question concerning the way you dynamically generate the message box: is it better to do it like this or predefine the whole thing (create and store the message box in a variable etc.) and then simply call it when needed?
@rbaleksandar It's better to use the QMessageBox static methods. Qt will clean up any memory used when the methods return, there is no need to keep one in memory permanently.
Thanks, it makes sense. After all this part of the UI is not something that 1)requires a lot of resources thus takes some time to load and 2)is often or even constantly on the screen for the user to see it.
Best answer ever.
A
AdvancedNewbie

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
}

Interesting answer, how would you add an icon to it? Like Information?
@Dariusz: you have the setIcon method part of QMessageBox object. with one if these enums as paramters: QMessageBox::NoIcon QMessageBox::Question QMessageBox::Information doc.qt.io/qt-4.8/qmessagebox.html#icon-prop
h
hkyi

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()) 
{

}

D
DomTomCat

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>


T
Toby Speight

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
        ;
}

Since you've already included QApplication I'd suggest using qApp->translate("context", "String"), which replaces tr for the use outside of a QObject class
p
peerpressure

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")


2
2 revs, 2 users 62%

If you want to make it in python you need check this code in your workbench. also write like this. we created a popup box with python.

msgBox = QMessageBox()
msgBox.setText("The document has been modified.")
msgBox.setInformativeText("Do you want to save your changes?")
msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
msgBox.setDefaultButton(QMessageBox.Save)
ret = msgBox.exec_()