QT之消息对话框

目录


一:QMessageBox消息对话框

QMessageBox 是 Qt 框架中常用的一个类,可以生成各式各样、各种用途的消息对话框,

QT之消息对话框_第1张图片

 

1.通用的QMessageBox消息框

Qt 提供了 6 种通用的 QMessageBox 消息对话框,通过调用 QMessageBox 类中的 6 个静态成员方法,可以直接在项目中使用它们。

1) information消息对话框

information 对话框常用于给用户提示一些关键的信息,它的外观如下图所示:

QT之消息对话框_第2张图片

在项目中使用 information 消息对话框,直接调用 QMessageBox 类中的 information() 静态成员方法即可,该方法的语法格式如下:

StandardButton QMessageBox::information(QWidget *parent,
                                        const QString &title,
                                        const QString &text,
                                        StandardButtons buttons = Ok,
                                        StandardButton defaultButton = NoButton)

 QT之消息对话框_第3张图片

例如,使用 information() 函数实现图 2 所示的对话框,实现代码为:

QMessageBox::StandardButton result = QMessageBox::information(&widget, "Title","text");

 2) critical消息对话框

critical 消息对话框常用于给用户提示“操作错误”或“运行失败”的信息,它的外观如下图所示:

QT之消息对话框_第4张图片

项目中使用 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");

3) question消息对话框

question 对话框常用于向用户提出问题并接收用户的答案,它的外观如下图所示:

QT之消息对话框_第5张图片

项目中使用 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");

4) warning消息对话框 

warining 对话框常用于向用户显示一些警告信息,它的外观如下图所示:

QT之消息对话框_第6张图片

 项目中使用 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");

 

5) about和aboutQt对话框 

about 对话框常常作为介绍某个产品或某项功能的临时窗口,它的外观如下图所示:

QT之消息对话框_第7张图片

项目中使用 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())

 


2.自定义QMessageBox对话框

以上 6 种通用的 QMessageBox 对话框,界面上的图片无法修改,按钮上的文字也无法修改(例如无法将 OK、No 改成中文)。如果想修改它们,就需要自定义一个 QMessageBox 对话框。

1.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();
}

2.QMessageBox对话框的使用 

QT之消息对话框_第8张图片


#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();
}

QT之消息对话框_第9张图片 

 

 

 3.QMessageBox的信号和槽

QT之消息对话框_第10张图片

 

3.代码引例

1.在.h文件中

#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

2.在main文件中

#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();
}



4.结果演示 GIF

 

你可能感兴趣的:(QT,qt5,c++)