Qt5开发从入门到精通——第四篇五节(消息对话框类)

欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


前言

本章节将会给大家带来消息对话框的详细使用方法

一、消息对话框概述

在实际的程序开发中,经常会用到各种各样的消息框来为用户提供一些提示或提醒, Qt 提供了 QMessageBox 类用千实现此项功能。常用的消息对话框包括 Question 消息框、 Information 消息框、 Warning 消息框、 Critical 消息框、 About (关于)消息框、 About (关于) Qt 消息框及 Custom (自定义)消息框。其中,Question 消息框、 Information 消息框、 Warning 消息框和 Critical 消息框的用法大同小异。这些消息框通常都包含为用户提供一些提醒或一些简单询问用的一个图标、一条提示信息及若干个按钮。 Question 消息框为正常的操作提供一个简单的询问: Information 消息框为正常的操作提供一个提示: Warning 消息框提醒用户发生了一个错误: Critical 消息框警告用户发生了一个严重错误。

二、消息框函数详解

2.1、Question消息框

Question 消息框使用 QMessageBox: :question() 函数完成,该函数形式如下:

StandardButton QMessageBox::question
(
	QWidget* parent,               //消息框的父窗口指针
	const QString& title,         //消息框的标题栏
	const QString& text,          //消息框的文字提示信息
	StandardButtons buttons=Ok,   //注(1)
	StandardButton defaultButton=NoButton  //注(2)
)

注:
(1) 填写希望在消息框中出现的按钮,可根据需要在标准按钮中选择,用 “I” 连写,默认为 QMessageBox::Ok 。QMessageBox 类提供了许多标准按钮,
如 QMessageBox::Ok 、 QMessageBox: :Close 、 QMessageBox: :Discard 等。虽然在此可以选择,但并不是随意选择的,应注意按常规成对出现。例如,通常Save 与 Discard 成对出现,而Abort 、 Retry 、 Ignore 则一起出现。
(2)默认按钮,即消息框出现时,焦点默认处于哪个按钮上。

2.2、Information消息框

Information 消息框使用 QMessageBox::information() 函数完成,该函数形式如下:

StandardButton QMessageBox::information
(
	QWidget *parent,                    //消息框的父窗口指针
	const QString& title,               //消息框的标题栏
	const QString& text,                //消息框的文字提示信息
	StandardButtons buttons=Ok,         //同Question消息框的注释内容
	StandardButton defaultButton=NoButton  //同Question消息框的注释内容
)

2.3、Warning消息框

Warning 消息框使用 QMessageBox : :warning() 函数完成,该函数形式如下:

StandardButton QMessageBox::warning
(
	QWidget* parent,            //消息框的父窗口指针
	const QString& title,       //消息框的标题栏
	const QString& text,        //消息框的文字提示信息
	StandardButtons buttons=Ok,     //同Question消息框的注释内容
	StandardButton defaultButton=NoButton  //同Question消息框的注释内容
)

2.4、Critical消息框

Critical 消息框使用 QMessageBox: :critical() 函数完成,该函数形式如下:

StandardButton QMessageBox::critical
(
	QWidget* parent,            //消息框的父窗口指针
	const QString& title,       //消息框的标题栏
	const QString& text,        //消息框的文字提示信息
	StandardButtons buttons=Ok,     //同Question消息框的注释内容
	StandardButton defaultButton=NoButton  //同Question消息框的注释内容
)

2.5、About消息框

About 消息框使用 QMessageBox : :about() 函数完成,该函数形式如下:

void QMessageBox::about
(
	QWidget* parent,         //消息框的父窗口指针
	const QString& title,   //消息框的标题栏
	const QString& text     //消息框的文字提示信息
)

2.6、About Qt消息框

About Qt 消息框使用 QMessageBox:: aboutQt()函数完成,该函数形式如下:

void QMessageBox::aboutQt
(
	QWidget*  parent,              //消息框的父窗口指针
	const QString&  title=QString() //消息框的标题栏
)

二、效果实例

图一
在这里插入图片描述

三、类的创建及原码详解

3.1、类的创建

(1)添加该工程的提供主要显示标准消息对话框界面的函数所在的文件,在"DialogExample"项目名上单击鼠标右键,在弹出的快捷菜单中选择“添加新文件…”选项,在弹出的对话框中选择 “C++ Class” 选项,单击 "Choose…”按钮,在弹出的对话框的 “Base class” 下拉列表框中输入基类名 “QDialog”, 在 “Class name” 文本框中输入类的名称 “MsgBoxDlg” 。
(2) 单击“下一步”按钮,单击“完成”按钮,在该工程中就添加了 “msgboxdlg.h” 头文
件和 “msgboxdlg.cpp” 源文件。

3.2、原码详解

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <msgboxdlg.h>
#include <QPushButton>
#include <QGridLayout>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
    QPushButton *MsgBtn;
    MsgBoxDlg   *msgDlg;
    QGridLayout *mainLayout;
private slots:
void showMsgDlg();

};

#endif // DIALOG_H

msgboxdlg.h

#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H

#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
class MsgBoxDlg : public QDialog
{
    Q_OBJECT
public:
    MsgBoxDlg(QWidget* parent=0);
private slots:
void showQuestionMsg();
void showinformationMsg();
void showWarningMsg();
void showCriticalMsg ();
void showAboutMsg();
void showAboutQtMsg();
private:
QLabel *label;
QPushButton *questionBtn;
QPushButton *informationBtn;
QPushButton *warningBtn;
QPushButton *criticalBtn;
QPushButton *aboutBtn;
QPushButton *aboutQtBtn;
QGridLayout *mainLayout;
};

#endif // MSGBOXDLG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    MsgBtn = new QPushButton;
    MsgBtn->setText(tr("标准消息对话框实例"));
    MsgBtn->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;"); //设置按钮形状
    //添加布局管理:
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(MsgBtn,3,1);
    connect (MsgBtn, SIGNAL (clicked()), this, SLOT (showMsgDlg()));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::showMsgDlg()
{
    msgDlg = new MsgBoxDlg();
    msgDlg->show();
}

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

msgboxdlg.cpp

#include "msgboxdlg.h"

MsgBoxDlg::MsgBoxDlg(QWidget *parent):QDialog (parent)
{
    setWindowTitle(tr("标准消息对话框实例"));     //设置对话框的标题
    label = new QLabel;
    label->setText(tr("请选择一种消息框"));
    questionBtn = new QPushButton;
    questionBtn->setText(tr("QuestionMsg"));
    informationBtn = new QPushButton;
    informationBtn->setText(tr("InformationMsg"));
    warningBtn = new QPushButton;
    warningBtn->setText(tr("WarningMsg"));
    criticalBtn = new QPushButton;
    criticalBtn->setText(tr("criticalMsg"));
    aboutBtn =new QPushButton;
    aboutBtn->setText(tr("AboutMsg"));
    aboutQtBtn = new QPushButton;
    aboutQtBtn->setText(tr("AboutQtMsg"));
    //布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(label,0,0,1,2);
    mainLayout->addWidget(questionBtn,1,0);
    mainLayout->addWidget(informationBtn,1,1);
    mainLayout->addWidget(warningBtn,2,0);
    mainLayout->addWidget(criticalBtn,2,1);
    mainLayout->addWidget(aboutBtn,3,0);
    mainLayout->addWidget(aboutQtBtn,3,1);
    //事件关联
    connect(questionBtn,SIGNAL(clicked()),this,SLOT(showQuestionMsg()));
    connect (informationBtn, SIGNAL (clicked()), this, SLOT(showinformationMsg ()));
    connect (warningBtn, SIGNAL (clicked()), this, SLOT(showWarningMsg()));
    connect (criticalBtn, SIGNAL (clicked()), this, SLOT (showCriticalMsg ()));
    connect (aboutBtn, SIGNAL (clicked()), this, SLOT (showAboutMsg ()));
    connect (aboutQtBtn, SIGNAL (clicked()), this, SLOT (showAboutQtMsg ()));


}

void MsgBoxDlg::showQuestionMsg()
{
    label->setText(tr("Question Message Box"));

   bool abc = QMessageBox::question(this,tr("Question 消息框"),
                                    tr(" 您现在已经修改完成,是否要结束程序?"),
                                    QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
    switch (abc)
    {
    case QMessageBox::Ok:
    label->setText("Question button/Ok");
    break;
    case QMessageBox::Cancel:
    label->setText("Question button/Cancel");
    break;
    default:
    break;
    }
    return;
}
void MsgBoxDlg::showinformationMsg()
{
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this,tr("Information 消息框"),
    tr(" 这是 Information 消息框测试,欢迎您!"));
    return;
}
void MsgBoxDlg::showWarningMsg()
{
    label->setText(tr("Warning Message Box"));
    switch(QMessageBox::warning(this,tr("Warning 消息框"),
    tr(" 您修改的内容还未保存,是否要保存对文档的修改?"),
    QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
    QMessageBox::Save))
    {
    case QMessageBox::Save:
    label->setText(tr("Warning button/Save"));
    break;
    case QMessageBox::Discard:
    label->setText(tr("Warning button/Discard"));
    break;
    case QMessageBox::Cancel:
    label->setText(tr("Warning button/Cancel"));
    break;
    default:
    break;
   }
     return;
}
void MsgBoxDlg::showCriticalMsg()
{
    label->setText(tr("Critical Message Box"));
    QMessageBox::critical(this,tr("Critical 消息框") ,tr("这是一个 Critical 消息框测试!"));
    return;
}
void MsgBoxDlg::showAboutMsg()
{
    label->setText(tr("About Message Box"));
    QMessageBox::about(this,tr("About消息框") ,tr(" 这是一个 About 消息框测试!"));
    return;
}
void MsgBoxDlg::showAboutQtMsg()
{
    label->setText(tr("About Qt Message Box"));
    QMessageBox::aboutQt(this,tr("About Qt消息框")) ;
    return;
}


总结

消息对话框也是在应用程序中经常用到的

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东.'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值