QT 消息框大全

 

/* messagebox.h
 * 孤舟钓客
 * 2011-07-16
 */

#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H

#include <QDialog>

class QLabel;

class MessageBox : public QDialog
{
    Q_OBJECT
public:
    MessageBox(QWidget *parent = 0);

public slots:
    void slotQuession();
    void slotInformation();
    void slotWarning();
    void slotCritical();
    void slotAbout();
    void slotAboutQt();
    void slotCustom();

private:
    QLabel *label;
};

#endif // MESSAGEBOX_H

/* messagebox.cpp
 * 孤舟钓客
 * 2011-07-16
 */

#include "messagebox.h"
#include <QtGui>

MessageBox::MessageBox(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("消息框示例"));

    label = new QLabel;

    QPushButton *btn1 = new QPushButton(tr("询问"));
    QPushButton *btn2 = new QPushButton(tr("提示"));
    QPushButton *btn3 = new QPushButton(tr("警告"));
    QPushButton *btn4 = new QPushButton(tr("错误"));
    QPushButton *btn5 = new QPushButton(tr("关于"));
    QPushButton *btn6 = new QPushButton(tr("关于 Qt"));
    QPushButton *btn7 = new QPushButton(tr("自定义"));

    QGridLayout *grid = new QGridLayout;
    grid->addWidget(btn1, 0, 0);
    grid->addWidget(btn2, 0, 1);
    grid->addWidget(btn3, 1, 0);
    grid->addWidget(btn4, 1, 1);
    grid->addWidget(btn5, 2, 0);
    grid->addWidget(btn6, 2, 1);
    grid->addWidget(btn7, 3, 0);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setMargin(10);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(label);
    mainLayout->addLayout(grid);
    setLayout(mainLayout);

    connect(btn1, SIGNAL(clicked()), this, SLOT(slotQuession()));
    connect(btn2, SIGNAL(clicked()), this, SLOT(slotInformation()));
    connect(btn3, SIGNAL(clicked()), this, SLOT(slotWarning()));
    connect(btn4, SIGNAL(clicked()), this, SLOT(slotCritical()));
    connect(btn5, SIGNAL(clicked()), this, SLOT(slotAbout()));
    connect(btn6, SIGNAL(clicked()), this, SLOT(slotAboutQt()));
    connect(btn7, SIGNAL(clicked()), this, SLOT(slotCustom()));
}

void MessageBox::slotQuession()
{
    switch(QMessageBox::question(this,
                                 tr("询问"),
                                 tr("已到达文档结尾,是否从头搜索?"),
                                 QMessageBox::Ok | QMessageBox::Cancel,
                                 QMessageBox::Ok))
    {
    case QMessageBox::Ok:
        label->setText(tr("询问按钮 / 确定 "));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("询问按钮 / 取消 "));
        break;
    default:
        break;
    }
    return;
}

void MessageBox::slotInformation()
{
    QMessageBox::information(this,
                             tr("提示"),
                             tr("填写任意想告诉用户的信息"));
    label->setText(tr(" 提示消息框 "));
    return;
}

void MessageBox::slotWarning()
{
    switch(QMessageBox::warning(this,
                                tr("警告"),
                                tr("是否保存对文档的修改?"),
                                QMessageBox::Save |
                                QMessageBox::Discard |
                                QMessageBox::Cancel,
                                QMessageBox::Save))
    {
    case QMessageBox::Save:
        label->setText(tr(" 警告按钮 / 保存 "));
        break;
    case QMessageBox::Discard:
        label->setText(tr(" 警告按钮 / 放弃 "));
        break;
    case QMessageBox::Cancel:
        label->setText(tr(" 警告按钮 / 取消 "));
        break;
    default:
        break;
    }
    return;
}

void MessageBox::slotCritical()
{
    QMessageBox::critical(this,
                          tr("Critical"),
                          tr("提醒用户一个致命的错误"));
    label->setText(tr(" 致命错误消息框 "));
    return;
}

void MessageBox::slotAbout()
{
    QMessageBox::about(this,
                       tr("关于"),
                       tr("QMessageBox 实例!"));
    label->setText(tr(" 关于消息框 "));
    return;
}

void MessageBox::slotAboutQt()
{
    QMessageBox::aboutQt(this,
                         tr("关于 Qt"));
    label->setText(tr(" 关于 Qt 消息框 "));
    return;
}

void MessageBox::slotCustom()
{
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(tr("自定义消息框"));

    QPushButton *lockButton = customMsgBox.addButton(tr("锁定"),
                                                     QMessageBox::ActionRole);
    QPushButton *unlockButton = customMsgBox.addButton(tr("解锁"),
                                                     QMessageBox::ActionRole);
    QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);

    customMsgBox.setIconPixmap(QPixmap("./images/linuxredhat.png"));
    customMsgBox.setText(tr("这是一个自定义的消息框\n 标题,图标,按钮,文字都可以自定义"));
    customMsgBox.exec();

    if (customMsgBox.clickedButton() == lockButton)
        label->setText(tr(" 自定义的消息框 / 锁定 "));
    if (customMsgBox.clickedButton() == unlockButton)
        label->setText(tr(" 自定义的消息框 / 解锁 "));
    if (customMsgBox.clickedButton() == cancelButton)
        label->setText(tr(" 自定义的消息框 / 取消 "));

    return;
}

/* main.cpp
 * 孤舟钓客
 * 2011-07-16
 */

#include <QApplication>
#include <QTranslator>
#include <QTextCodec>
#include "messagebox.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QFont f("ZYSong18030", 12);
    QApplication::setFont(f);

    QTranslator translator;
    translator.load("messagebox_zh");
    app.installTranslator(&translator);

    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));

    MessageBox msg;
    msg.show();

    return app.exec();
}

QT 消息框大全_第1张图片

QT 消息框大全_第2张图片

QT 消息框大全_第3张图片

QT 消息框大全_第4张图片

QT 消息框大全_第5张图片


你可能感兴趣的:(文档,qt,Signal)