是/否消息框使用QMessageBox [英] Yes/No message box using QMessageBox
问题描述
如何在Qt中显示带有是/否按钮的消息框,以及如何检查哪些按钮已按下?
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:
推荐答案
You would use QMessageBox::question
for that.
#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";
}
}
在Win32上需要 QT + = widgets
,在$ Win32上需要 CONFIG + = console
才能看到 qDebug()
输出。
Should work on Qt 4 and 5, requires QT += widgets
on Qt 5, and CONFIG += console
on Win32 to see qDebug()
output.
请参阅 StandardButton
枚举以获取可以使用的按钮列表;该函数返回所单击的按钮。您可以设置一个带有额外参数的默认按钮(Qt如果没有指定,则自动选择合适的默认值 )。
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
).
这篇关于是/否消息框使用QMessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!