【Qt】消息对话框类

来源:互联网 发布:张孝祥javascript视频 编辑:程序博客网 时间:2024/06/10 01:55

消息对话框类在前者消息对话框的基础之上完成的

参见:http://blog.csdn.net/ldan508/article/details/51921182


【效果如下】

     



【添加代码】

添加新的文件msgboxdlg类

//msgboxdlg.h

<span style="font-family:Microsoft YaHei;font-size:18px;">#ifndef MSGBOXDLG_H#define MSGBOXDLG_H#include<QLabel>#include<QPushButton>#include<QGridLayout>#include<QDialog>class MsgBoxDlg : public QDialog{    Q_OBJECTpublic:    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</span>


//msgboxdlg.cpp

<span style="font-family:Microsoft YaHei;font-size:18px;">#include "msgboxdlg.h"MsgBoxDlg::MsgBoxDlg(QWidget *parent) :    QDialog(parent){    setWindowTitle(tr("标注信息对话框的实例"));  //设置对话框的标题    label = new QLabel;    label->setText(tr("请选择一种消息框"));    questionBtn = new QPushButton;    questionBtn->setText(tr("QusetionMsg"));    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(bool)),this,SLOT(showQuestionMsg()));    connect(informationBtn,SIGNAL(clicked(bool)),this,SLOT(showInformationMsg()));    connect(warningBtn,SIGNAL(clicked(bool)),this,SLOT(showWarningMsg()));\    connect(criticalBtn,SIGNAL(clicked(bool)),this,SLOT(showCriticalMsg()));    connect(aboutBtn,SIGNAL(clicked(bool)),this,SLOT(showAboutMsg()));    connect(aboutQtBtn,SIGNAL(clicked(bool)),this,SLOT(showAboutQtMsg()));}void MsgBoxDlg::showQuestionMsg(){}void MsgBoxDlg::showInformationMsg(){}void MsgBoxDlg::showWarningMsg(){}void MsgBoxDlg::showCriticalMsg(){}void MsgBoxDlg::showAboutMsg(){}void MsgBoxDlg::showAboutQtMsg(){}</span>


//dialog.h


<span style="font-family:Microsoft YaHei;font-size:18px;"> 添加头文件#include "msgboxdlg.h" 添加private成员变量: QPushButton *MsgBtn;添加类函数: MsgBoxDlg *msgDlg;添加槽函数:void showMsgDlg();</span>


//dialog.cpp

<span style="font-family:Microsoft YaHei;font-size:18px;"> 构造函数中添加: MsgBtn =new QPushButton; MsgBtn->setText(tr("标准消息对话框实例"));   mainLayout->addWidget(MsgBtn,3,1);   connect(MsgBtn,SIGNAL(clicked(bool)),this,SLOT(showMsgDlg()));槽函数的实现:void Dialog::showMsgDlg(){    msgDlg =new MsgBoxDlg();    msgDlg->show();}</span>


0 0
原创粉丝点击