C++ 是/否消息框使用 QMessageBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13111669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:58:41  来源:igfitidea点击:

Yes/No message box using QMessageBox

c++qtqmessagebox

提问by sashoalm

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

如何在 Qt 中显示带有 Yes/No 按钮的消息框,以及如何检查按下了哪个按钮?

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

即一个消息框,看起来像这样:

enter image description here

在此处输入图片说明

回答by Mat

You would use QMessageBox::questionfor that.

你会用QMessageBox::question它。

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 += widgetson Qt 5, and CONFIG += consoleon Win32 to see qDebug()output.

应该在 Qt 4 和 5 上工作,需要QT += widgets在 Qt 5 和CONFIG += consoleWin32 上查看qDebug()输出。

See the StandardButtonenum 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).

查看StandardButton枚举以获取您可以使用的按钮列表;该函数返回被点击的按钮。您可以使用额外的参数设置默认按钮(如果您没有或指定,Qt“会自动选择合适的默认值QMessageBox::NoButton)。

回答by rednaks

You can use the QMessage object to create a Message Box then add buttons :

您可以使用 QMessage 对象创建一个消息框,然后添加按钮:

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
}

回答by hkyi

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

QT 可以像 Windows 一样简单。等效代码是

if (QMessageBox::Yes == QMessageBox(QMessageBox::Information, "title", "Question", QMessageBox::Yes|QMessageBox::No).exec()) 
{

}

回答by DomTomCat

I'm missing the translation call trin the answers.

我错过tr了答案中的翻译电话。

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 Qthabit to put code-level Strings within a tr("Your String")call.

Qttr("Your String")调用中放置代码级字符串通常是一个好习惯。

(QMessageboxas above works within any QWidgetmethod)

QMessagebox如上适用于任何QWidget方法)

EDIT:

编辑:

you can use QMesssageBoxoutside a QWidgetcontext, see @TobySpeight's answer.

您可以QMesssageBoxQWidget上下文之外使用,请参阅@TobySpeight 的回答。

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

如果您甚至不在QObject上下文中,请替换trqApp->translate("context", "String")- 您需要#include <QApplication>

回答by Toby Speight

QMessageBoxincludes static methods to quickly ask such questions:

QMessageBox包括可以快速提出此类问题的静态方法:

#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 QMessageBoxobject, 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:

如果您的需求比静态方法提供的更复杂,您应该构造一个新QMessageBox对象,并调用它的exec()方法在它自己的事件循环中显示它并获取按下的按钮标识符。例如,我们可能希望将“否”设为默认答案:

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

回答by Rajendra Prasad Taidala

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.

如果你想在 python 中制作它,你需要在你的工作台中检查这段代码。也这样写。我们用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_()