Qt消息框

//msgDialog.h

#ifndef MSGDIALOG_H
#define MSGDIALOG_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>

class MsgDialog : public QDialog
{
    Q_OBJECT
public:
    MsgDialog(QWidget *parent = 0);
    ~MsgDialog();
private slots:
    void showQuestionMsg();
    void showInfomationMsg();
    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 // MSGDIALOG_H

//msgDialog.cpp

#include "msgdialog.h"

MsgDialog::MsgDialog(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(showInfomationMsg()));
    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()));
}

MsgDialog::~MsgDialog()
{

}

void MsgDialog::showQuestionMsg()
{
#if 0
    QMessageBox::question(
                父窗,消息標題,文字提示,消息框中希望出現的按鈕,默認焦點在哪個按鈕);
#endif
    label->setText(tr("問題消息框"));
    switch(QMessageBox::question(this,tr("question"),tr("您已經修改完成,是否要結束程序?"),QMessageBox::Ok | QMessageBox::Cancel,
                                 QMessageBox::Ok))
    {
    case QMessageBox::Ok:
        label->setText(tr("Question is ok"));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("Question is Cancel"));
        break;
    default:
        break;
    }
}

void MsgDialog::showInfomationMsg()
{
#if 0
    QMessageBox::information(
                父窗,消息標題,文字提示,消息框中希望出現的按鈕,默認焦點在哪個按鈕);
#endif

    label->setText(tr("information 消息框"));
    switch(QMessageBox::information(this,tr("information"),tr("註釋信息"),QMessageBox::Ok | QMessageBox::Cancel,
                                 QMessageBox::Ok))
    {
    case QMessageBox::Ok:
        label->setText(tr("information is ok"));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("information is Cancel"));
        break;
    default:
        break;
    }
}

void MsgDialog::showWarningMsg()
{
#if 0
    QMessageBox::warning(
                父窗,消息標題,文字提示,消息框中希望出現的按鈕,默認焦點在哪個按鈕);
#endif

    label->setText(tr("warning 消息框"));
    switch(QMessageBox::warning(this,tr("information"),tr("修改的內容未保存,是否保存?"),QMessageBox::Save|QMessageBox::Discard | QMessageBox::Cancel,
                                 QMessageBox::Save))
    {
    case QMessageBox::Save:
        label->setText(tr("warning is Save"));
        break;
    case QMessageBox::Discard:
        label->setText(tr("warning is Discard"));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("warning is Cancel"));
        break;
    default:
        break;
    }
}

void MsgDialog::showCriticalMsg()
{
#if 0
    QMessageBox::critical(
                父窗,消息標題,文字提示,消息框中希望出現的按鈕,默認焦點在哪個按鈕);
#endif

    label->setText(tr("critical 消息框"));
    switch(QMessageBox::critical(this,tr("critical"),tr("critical 消息框測試"),QMessageBox::Ok | QMessageBox::Cancel,
                                 QMessageBox::Ok))
    {
    case QMessageBox::Ok:
        label->setText(tr("critical is ok"));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("critical is Cancel"));
        break;
    default:
        break;
    }
}

void MsgDialog::showAboutMsg()
{
#if 0
    QMessageBox::about(
                父窗,消息標題,文字提示);
#endif

    label->setText(tr("about 消息框"));
    QMessageBox::about(this,tr("about"),tr("about 消息框測試"));

}

void MsgDialog::showAboutQtMsg()
{
#if 0
    QMessageBox::about(
                父窗,消息標題);
#endif

    label->setText(tr("aboutQt 消息框"));
    QMessageBox::aboutQt(this,tr("aboutQt"));

}

//dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "msgdialog.h"

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton *MsgBtn;
    QGridLayout *mainLayout;
    MsgDialog *msgDlg;

private slots:
    void showMsgDlg();
};

#endif // DIALOG_H

//dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    MsgBtn = new QPushButton;
    MsgBtn->setText(tr("標準消息對話框實例"));

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(MsgBtn,3,1);

    connect(MsgBtn,SIGNAL(clicked()),this,SLOT(showMsgDlg()));

}

Dialog::~Dialog()
{

}

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章