目录
QMessageBox 是 Qt 框架中常用的一个类,可以生成各式各样、各种用途的消息对话框,
Qt 提供了 6 种通用的 QMessageBox 消息对话框,通过调用 QMessageBox 类中的 6 个静态成员方法,可以直接在项目中使用它们。
information 对话框常用于给用户提示一些关键的信息,它的外观如下图所示:
在项目中使用 information 消息对话框,直接调用 QMessageBox 类中的 information() 静态成员方法即可,该方法的语法格式如下:
StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
例如,使用 information() 函数实现图 2 所示的对话框,实现代码为:
QMessageBox::StandardButton result = QMessageBox::information(&widget, "Title","text");
critical 消息对话框常用于给用户提示“操作错误”或“运行失败”的信息,它的外观如下图所示:
项目中使用 critical 消息对话框,直接调用 QMessageBox 类提供的 critical() 静态成员方法即可,该方法的语法格式为:
StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
例如,使用 critical() 函数实现图 3 所示的对话框,实现代码为:
QMessageBox::StandardButton result=QMessageBox::critical(&widget, "Title","text");
question 对话框常用于向用户提出问题并接收用户的答案,它的外观如下图所示:
项目中使用 question 对话框,可以直接调用 QMessageBox 类的 question() 静态成员方法,该方法的语法格式为:
StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
例如,使用 question() 函数实现图 4 所示的对话框,实现代码为:
QMessageBox::StandardButton result=QMessageBox::question(&widget, "Title","text");
warining 对话框常用于向用户显示一些警告信息,它的外观如下图所示:
项目中使用 warning 对话框,可以直接调用 QMessageBox 类的 warning() 静态成员方法,该方法的语法格式为:
StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
例如,使用 warning() 函数实现图 5 所示的对话框,实现代码为:
QMessageBox::StandardButton result=QMessageBox::warning(&widget, "Title","text");
about 对话框常常作为介绍某个产品或某项功能的临时窗口,它的外观如下图所示:
项目中使用 about 对话框,直接调用 QMessageBox 类提供的 about() 静态成员方法即可,该方法的语法格式如下:
void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
项目中使用 aboutQt 对话框,直接调用 QMessageBox 类提供的 aboutQt() 静态成员方法即可,该函数的语法格式如下:
void QMessageBox::aboutQt(QWidget *parent, const QString &title = QString())
以上 6 种通用的 QMessageBox 对话框,界面上的图片无法修改,按钮上的文字也无法修改(例如无法将 OK、No 改成中文)。如果想修改它们,就需要自定义一个 QMessageBox 对话框。
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建 QMessageBox 类对象
QMessageBox MyBox(QMessageBox::Question,"Title","text",QMessageBox::Yes|QMessageBox::No);
//使 MyBox 对话框显示
MyBox.exec();
return a.exec();
}
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox MBox;
MBox.setWindowTitle("QMessageBox自定义对话框");
MBox.setText("这是一个自定义的对话框");
MBox.setIconPixmap(QPixmap("D:\\Captura\\QT\\微信.png"));
QPushButton *agreeBut = MBox.addButton("同意", QMessageBox::AcceptRole);
MBox.exec();
if (MBox.clickedButton() == (QAbstractButton*)agreeBut) {
//在 Qt Creator 的输出窗口中输出指定字符串
qDebug() << "用户点击了同意按钮";
}
return a.exec();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
class MyWidget:public QWidget{
Q_OBJECT
public slots:
void buttonClicked(QAbstractButton *butClicked);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include
#include
QPushButton *agreeBut;
QPushButton *disagreeBut;
void MyWidget::buttonClicked(QAbstractButton *butClicked){
if(butClicked == (QAbstractButton*)disagreeBut){
this->close();
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建主窗口
MyWidget mywidget;
mywidget.setWindowTitle("主窗口");
mywidget.setGeometry(930,175,400,300);
//创建消息框
QMessageBox MyBox(QMessageBox::Question,"","");
MyBox.setParent(&mywidget);
//设置消息框的属性为对话框,他会是一个独立的窗口
MyBox.setWindowFlag(Qt::Dialog);
MyBox.setWindowTitle("协议");
MyBox.setText("使用本产品,需进行实名认证");
//自定义两个按钮
agreeBut = MyBox.addButton("同意",QMessageBox::AcceptRole);
disagreeBut = MyBox.addButton("拒绝",QMessageBox::RejectRole);
//添加信号和槽,监听用户点击的按钮,如果用户拒绝,则主窗口随之关闭。
QObject::connect(&MyBox,&QMessageBox::buttonClicked,&mywidget,&MyWidget::buttonClicked);
mywidget.show();
MyBox.exec();
return a.exec();
}