阅读导航
- 引言
- 一、消息对话框简介
- 二、问题提示消息对话框创建
- 三、信息提示消息对话框创建
- 四、警告信息消息对话框创建
- 五、错误提示消息对话框创建
引言
在上一篇文章中,我们一同探索了Qt框架中窗口与对话框的奥秘,特别是那些由Qt内置提供的、功能丰富且易于使用的对话框类型。这些对话框不仅简化了与用户交互的过程,还极大地提升了应用程序的可用性和用户体验。今天,我们将深入这一话题,聚焦于Qt中一个极其重要且频繁使用的组件——QMessageBox消息对话框。
一、消息对话框简介
消息对话框是应用程序中极为常见的界面元素,它们的主要作用是向用户展示重要信息,并有时强制用户进行必要的选择或操作。在Qt框架中,QMessageBox类专门为此目的而设计,它提供了一系列静态成员函数,允许开发者直接调用这些函数来快速创建并展示具有不同风格(如信息、警告、错误、询问等)的消息对话框。

| 消息类型 | 用途 | 
|---|---|
| Question | 用于正常操作过程中的提问 | 
| Information | 用于报告正常运行信息 | 
| Warning | 用于报告非关键错误 | 
| Critical | 用于报告严重错误 | 
二、问题提示消息对话框创建
💻代码
#include "mainwindow.h"  
#include <QMessageBox>  
#include <QPushButton>  
  
MainWindow::MainWindow(QWidget *parent)  
    : QMainWindow(parent)  
{  
    resize(800, 600);  
    QPushButton *btn = new QPushButton("消息对话框", this);  
    QMessageBox *msg = new QMessageBox(this);  
    msg->setWindowTitle("Warning Message"); // 设置消息对话框的标题  
    msg->setText("Error Message!"); // 设置消息对话框的内容  
    msg->setIcon(QMessageBox::Question); // 设置消息对话框类型  
    msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); // 在消息对话框上设置按钮  
    connect(btn, &QPushButton::clicked, [=]() {  
        msg->show();  
    });  
}
🎯实现效果
 
 其中可以设置的按钮的类型如下:

三、信息提示消息对话框创建
💻代码
#include "mainwindow.h"  
#include <QMessageBox>  
#include <QPushButton>  
  
MainWindow::MainWindow(QWidget *parent)  
    : QMainWindow(parent)  
{  
    resize(800, 600);  
    QPushButton *btn = new QPushButton("消息对话框", this);  
    QMessageBox *msg = new QMessageBox(this);  
    msg->setWindowTitle("Warning Message"); // 设置消息对话框的标题  
    msg->setText("Error Message!"); // 设置消息对话框的内容  
    msg->setIcon(QMessageBox::Warning); // 设置消息对话框类型  
    msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); // 在消息对话框上设置按钮  
    connect(btn, &QPushButton::clicked, [=]() {  
        msg->show();  
    });  
}
🎯实现效果

四、警告信息消息对话框创建
💻代码
#include "mainwindow.h"  
#include <QMessageBox>  
#include <QPushButton>  
  
MainWindow::MainWindow(QWidget *parent)  
    : QMainWindow(parent)  
{  
    resize(800, 600);  
    QPushButton *btn = new QPushButton("消息对话框", this);  
    QMessageBox *msg = new QMessageBox(this);  
    msg->setWindowTitle("Warning Message"); // 设置消息对话框的标题  
    msg->setText("Error Message!"); // 设置消息对话框的内容  
    msg->setIcon(QMessageBox::Question); // 设置消息对话框类型  
    msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); // 在消息对话框上设置按钮  
    connect(btn, &QPushButton::clicked, [=]() {  
        msg->show();  
    });  
}
🎯实现效果
 
五、错误提示消息对话框创建
💻代码
#include "mainwindow.h"  
#include <QMessageBox>  
#include <QPushButton>  
  
MainWindow::MainWindow(QWidget *parent)  
    : QMainWindow(parent)  
{  
    resize(800, 600);  
    QPushButton *btn = new QPushButton("消息对话框", this);  
    QMessageBox *msg = new QMessageBox(this);  
    msg->setWindowTitle("Warning Message"); // 设置消息对话框的标题  
    msg->setText("Error Message!"); // 设置消息对话框的内容  
    msg->setIcon(QMessageBox::Critical); // 设置消息对话框类型  
    msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); // 在消息对话框上设置按钮  
    connect(btn, &QPushButton::clicked, [=]() {  
        msg->show();  
    });  
}
🎯实现效果
 



















