基本描述
QMessageBox类提供了一个有一条简短消息、一个图标和一些按钮的模式对话框。
消息框用于提供情报信息并且问一些简单的问题。
QMessageBox提供了一个不同信息的范围,大致按两个轴进行排列:严重程度和复杂程度。
按严重程度分:消息;警告;错误。
按复杂程度分为一个按钮(确定)的简单消息、或者用于提问的两个或者甚至三个按钮。
简单使用
1、信息窗口
[static] int QMessageBox::information(QWidget *parent, const QString &title,
const QString &text,
int button0, int button1 = 0, int button2 = 0)
例:
int ret=QMessageBox::information(this, tr("My Application"),
tr("The time is over.\n"
"Do you complete?"),
QMessageBox::Yes | QMessageBox::No);
2、警告窗口
[static] int QMessageBox::warning(QWidget *parent, const QString &title,
const QString &text,
int button0, int button1, int button2 = 0)
例:
int ret=QMessageBox::warning(this, tr("My Application"),
tr("The schedule is empty.\n"
"Add a schedule to the text."),
QMessageBox::Ok);
或者自定义QMessageBox对象,使用成员函数。
例:
QMessageBox msgBox;
msgBox.setText("The time is not over.");
msgBox.setInformativeText("Do you want to cancel this schedule?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = msgBox.exec();