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
Yes/No message box using QMessageBox
提问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:
即一个消息框,看起来像这样:
回答by 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.
应该在 Qt 4 和 5 上工作,需要QT += widgets
在 Qt 5 和CONFIG += console
Win32 上查看qDebug()
输出。
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
).
查看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 tr
in 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 Qt
habit to put code-level Strings within a tr("Your String")
call.
Qt
在tr("Your String")
调用中放置代码级字符串通常是一个好习惯。
(QMessagebox
as above works within any QWidget
method)
(QMessagebox
如上适用于任何QWidget
方法)
EDIT:
编辑:
you can use QMesssageBox
outside a QWidget
context, see @TobySpeight's answer.
您可以QMesssageBox
在QWidget
上下文之外使用,请参阅@TobySpeight 的回答。
If you're even outside a QObject
context, replace tr
with qApp->translate("context", "String")
- you'll need to #include <QApplication>
如果您甚至不在QObject
上下文中,请替换tr
为qApp->translate("context", "String")
- 您需要#include <QApplication>
回答by Toby Speight
QMessageBox
includes 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 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:
如果您的需求比静态方法提供的更复杂,您应该构造一个新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_()