QMessageBox中的四种对话框

QMessageBox中的四种对话框

ui_dialogtest.h文件:

/********************************************************************************
** Form generated from reading UI file 'dialogtest.ui'
**
** Created by: Qt User Interface Compiler version 4.8.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_DIALOGTEST_H
#define UI_DIALOGTEST_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QHeaderView>
#include <QtGui/QPushButton>
#include <QtGui/QSplitter>

QT_BEGIN_NAMESPACE

class Ui_Dialog
{
public:
    QSplitter *splitter;
    QPushButton *informationButton;
    QPushButton *questionButton;
    QPushButton *warningButton;
    QPushButton *criticalButton;

    void setupUi(QDialog *Dialog)
    {
        if (Dialog->objectName().isEmpty())
            Dialog->setObjectName(QString::fromUtf8("Dialog"));
        Dialog->resize(337, 95);
        splitter = new QSplitter(Dialog);
        splitter->setObjectName(QString::fromUtf8("splitter"));
        splitter->setGeometry(QRect(10, 30, 300, 23));
        splitter->setOrientation(Qt::Horizontal);
        informationButton = new QPushButton(splitter);
        informationButton->setObjectName(QString::fromUtf8("informationButton"));
        splitter->addWidget(informationButton);
        questionButton = new QPushButton(splitter);
        questionButton->setObjectName(QString::fromUtf8("questionButton"));
        splitter->addWidget(questionButton);
        warningButton = new QPushButton(splitter);
        warningButton->setObjectName(QString::fromUtf8("warningButton"));
        splitter->addWidget(warningButton);
        criticalButton = new QPushButton(splitter);
        criticalButton->setObjectName(QString::fromUtf8("criticalButton"));
        splitter->addWidget(criticalButton);

        retranslateUi(Dialog);

        QMetaObject::connectSlotsByName(Dialog);
    } // setupUi

    void retranslateUi(QDialog *Dialog)
    {
        Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
        informationButton->setText(QApplication::translate("Dialog", "\344\277\241\346\201\257", 0, QApplication::UnicodeUTF8));
        questionButton->setText(QApplication::translate("Dialog", "\346\217\220\351\227\256", 0, QApplication::UnicodeUTF8));
        warningButton->setText(QApplication::translate("Dialog", "\350\255\246\345\221\212", 0, QApplication::UnicodeUTF8));
        criticalButton->setText(QApplication::translate("Dialog", "\344\270\245\351\207\215\351\224\231\350\257\257", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class Dialog: public Ui_Dialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_DIALOGTEST_H

dialogtest.h文件

#ifndef DIALOGTEST_H

#define DIALOGTEST_H
#include <QDialog>
#include "ui_dialogtest.h"
class dialogTest : public QDialog,public Ui_Dialog
{
    Q_OBJECT
public:
    dialogTest(QWidget *parent = 0);
    private slots:
        void clickInformationButton();
        void clickWarningButton();
        void clickQuestionButton();
        void clickCriticalButton();
};
#endif

dialogtest.c++文件

#include <QtGui>
#include <QMessageBox>
#include "dialogtest.h"
dialogTest::dialogTest(QWidget *parent): QDialog(parent)
{
    setupUi(this);
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb2312"));
    warningButton->setEnabled(false);
    connect(informationButton, SIGNAL(clicked()), this, SLOT(clickInformationButton()));
    connect( warningButton, SIGNAL(clicked()), this, SLOT(clickWarningButton()));
    connect(questionButton, SIGNAL(clicked()), this, SLOT(clickQuestionButton()));
    connect(criticalButton, SIGNAL(clicked()), this, SLOT(clickCriticalButton()));
}
void dialogTest::clickCriticalButton(){
     int r = QMessageBox::critical(this, "严重警告","严重警告按钮将变成灰常",QMessageBox::Ok);
     if (QMessageBox::Ok == r){
         criticalButton->setEnabled(false);
         warningButton->setEnabled(true);
     }
}
void dialogTest::clickInformationButton()
{
    QMessageBox::information(this, "信息","信息按钮会变化",QMessageBox::Ok);
    QString s = informationButton->text();
    if ("信息" == s)
           informationButton->setText("informtion");
    else
        informationButton->setText("信息");

}
void dialogTest::clickQuestionButton(){
    int r = QMessageBox::question(this, "提问","你确定退出吗?",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
    if (QMessageBox::Ok == r)
        close();
}
void dialogTest::clickWarningButton(){
 int r = QMessageBox::warning(this, "警告","需要严重警告按钮回复正常吗",QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel);
 if (QMessageBox::Ok == r){
     criticalButton->setEnabled(true);
     warningButton->setEnabled(false);
 }

}

 

main.c++文件:

 

include <QtGui/QApplication>
#include "dialogtest.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    dialogTest w;
    w.show();
    return a.exec();
}

posted @ 2014-06-26 16:43  sxcww  阅读(5721)  评论(0编辑  收藏  举报