下面是 QMessageBox 的 实例:
开发环境: VS 2013, Qt版本: 5.4.0
下面附上代码:
#include <QtWidgets/QApplication> #include <QMessageBox> int main(int argc, char *argv[]) { QApplication a(argc, argv); ///---实例化消息对话框对象 QMessageBox *msgBox; msgBox = new QMessageBox("title", ///--这里是设置消息框标题 "messageBox comment", ///--这里是设置消息框显示的内容 QMessageBox::Critical, ///--这里是在消息框显示的图标 QMessageBox::Ok | QMessageBox::Default, ///---这里是显示消息框上的按钮情况 QMessageBox::Cancel | QMessageBox::Escape, ///---这里与 键盘上的 escape 键结合。当用户按下该键,消息框将执行cancel按钮事件 0); ///---这里是 定义第三个按钮, 该例子 只是 了显示2个按钮 msgBox->show(); ///---显示消息框 return a.exec(); }当然,除了 OK 和 Cancel 按钮外, 还可选择其它按钮。例如:
QMessageBox ::Yes, QMessageBox :: No, QMessageBox :: Retry , QMessageBox :: Ignore。
消息框里显示的图标当然也不止一种,
◆ QMessageBox :: NoIcon不显示任何图标;
◆ QMessageBox :: Information显示一个信息图标
◆ QMessageBox :: warning显示一个警告图标
◆ QMessageBox :: Critical提示显示关键信息,可选用该图标
QMessageBox 还有以下这种比较常用的方法
QMessageBox::information(NULL, "title", "comment");
这里是该函数的原型:
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton);