11、QMessageBox(消息对话框)

发布时间 2023-12-08 14:12:20作者: 秃头的C#

 

 

 

 

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

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{


    ui->setupUi(this);
    resize(320,150);
    glayout = new QGridLayout(this);
    dispLabel = new QLabel("请选择标签");
    questionButton= new QPushButton("Question");
    informationButton= new QPushButton("Information");
    warningButton= new QPushButton("Warning");
    criticalButton= new QPushButton("Critical");
    aboutButton= new QPushButton("About");
    aboutqtButton= new QPushButton("AboutQt");

    glayout->addWidget(dispLabel,0,0 );
    glayout->addWidget(questionButton,1,0);
    glayout->addWidget(informationButton,1,1);
    glayout->addWidget(warningButton,2,0);
    glayout->addWidget(criticalButton,2,1);
    glayout->addWidget(aboutButton,3,0);
    glayout->addWidget(aboutqtButton,3,1);


    connect(questionButton,SIGNAL(clicked()),this,SLOT(displayQuestionMsg()));
    connect(informationButton,SIGNAL(clicked()),this,SLOT(displayInformationMsg()));
    connect(warningButton,SIGNAL(clicked()),this,SLOT(displayWarningMsg()));
    connect(criticalButton,SIGNAL(clicked(bool)),this,SLOT(displayCriticalMsg()));
    connect(aboutButton,SIGNAL(clicked(bool)),this,SLOT(displayAboutMsg()));
    connect(aboutqtButton,SIGNAL(clicked(bool)),this,SLOT(displayAboutQtMsg()));
}

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

void Dialog::displayQuestionMsg()
{
    dispLabel->setText("Question");
    switch(QMessageBox::question(this,"Question消息框","你是否想退出应用程序",
                                 QMessageBox::Ok|QMessageBox::Cancel
                                 ))
    {
    case QMessageBox::Ok:
        dispLabel->setText("Question->Ok");
        break;
    case QMessageBox::Cancel:
        dispLabel->setText("Question->cancel");
        break;
    default:
        break;
    }
    return;
}

void Dialog::displayInformationMsg()
{
    dispLabel->setText("Infomation");
    QMessageBox::information(this,"Infomation消息框","Infomation测试");
}

void Dialog::displayWarningMsg()
{
    dispLabel->setText("warning");
    switch (QMessageBox::warning(this,"Warning消息框","Warning测试",QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save)) {
    case QMessageBox::Save:
        dispLabel->setText("warning->save");
        break;
    case QMessageBox::Discard:
        dispLabel->setText("warning->Discard");
        break;
    case QMessageBox::Cancel:
        dispLabel->setText("warning->Cancel");
        break;
    default:
        break;
    }
}

void Dialog::displayCriticalMsg()
{
    dispLabel->setText("cirtical");
    QMessageBox::critical(this,"cirtical消息框","cirtical测试");
}

void Dialog::displayAboutMsg()
{
dispLabel->setText("About");
QMessageBox::about(this,"about消息框","about测试");
}

void Dialog::displayAboutQtMsg()
{
    dispLabel->setText("AboutQT");
    QMessageBox::aboutQt(this,"about QT消息框测试");
}