1.QMessageBox作用:生成信息提示框
头文件:
#include <QMessageBox>
2.用法:
2.1 QMessageBox::information
QMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton)
参数说明:
- parent:消息框的父窗口,一般为当前窗口。
- title:消息框的标题。
- text:消息框显示的文本内容。
- buttons:消息框上显示的按钮,可以是QMessageBox::Ok、QMessageBox::Cancel等。
- defaultButton:设定默认按钮,一般不需要设置。
示例:
QMessageBox::information(NULL,"information","请选择是否返回",QMessageBox::No|QMessageBox::Yes,QMessageBox::Yes);
2.2 QMessageBox::warning,通常用于提示用户一些警告信息
QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton)
参数说明:
- parent:消息框的父窗口,一般为当前窗口。
- title:消息框的标题。
- text:消息框显示的文本内容。
- buttons:消息框上显示的按钮,可以是QMessageBox::Ok、QMessageBox::Cancel等。
- defaultButton:设定默认按钮,一般不需要设置。
示例:
QMessageBox::warning(NULL,"warning","文件复警告",QMessageBox::No|QMessageBox::Yes,QMessageBox::Yes);
2.3QMessageBox::critical()通常用于提示用户一些错误信息或者异常情况。该函数的语法如下:
QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton)
参数说明:
- parent:消息框的父窗口,一般为当前窗口。
- title:消息框的标题。
- text:消息框显示的文本内容。
- buttons:消息框上显示的按钮,可以是QMessageBox::Ok、QMessageBox::Cancel等。
- defaultButton:设定默认按钮,一般不需要设置。
示例:
QMessageBox::critical(NULL,"critical","文件复制出错,请检查",QMessageBox::No|QMessageBox::Yes,QMessageBox::Yes);
2.4QMessageBox::question()通常用于提示用户进行二选一的选择。该函数的语法如下:
QMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Yes | QMessageBox::No, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton)
参数说明:
- parent:消息框的父窗口,一般为当前窗口。
- title:消息框的标题。
- text:消息框显示的文本内容。
- buttons:消息框上显示的按钮,可以是QMessageBox::Ok、QMessageBox::Cancel等。
- defaultButton:设定默认按钮,一般不需要设置。
示例:
QMessageBox::question(NULL,"question","你的学历是硕士研究生?",QMessageBox::No|QMessageBox::Yes,QMessageBox::Yes);
2.5QMessageBox::about()通常用于展示关于应用程序的信息。该函数的语法如下:
void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
参数说明:
- parent:消息框的父窗口,一般为当前窗口。
- title:消息框的标题。
- text:消息框显示的文本内容。
示例
QMessageBox::about(NULL,"about","默认提示框");
3.类对象生成。
QMessageBox msssBox(QMessageBox::NoIcon,"登录","验证密码是否正确",QMessageBox::No|QMessageBox::Yes,NULL);
参数:
msssBox:对象名称
第一个参数:消息框的类型,warning、information等
第二个参数:消息框的标题。
第三个参数:消息框显示的文本内容。
第四参数:消息框上显示的按钮,可以是QMessageBox::Ok、QMessageBox::Cancel等。
在Qt中,可以使用QMessageBox的exec()函数来显示消息框,并返回用户选择的结果。在你的代码中,使用msssBox.exec()函数来显示消息框并等待用户选择。这个函数会阻塞当前线程,直到用户点击了消息框上的按钮。
int res=msssBox.exec();
switch(res)
{
case QMessageBox::Yes:
QMessageBox::about(NULL,"about","你已选择yes");
break;
case QMessageBox::No:
QMessageBox::about(NULL,"about","你已选择no");
break;
}