Qt5——基本對話框(1)


Qt中的各種對話框應用

通過一個實例來介紹Qt5中對於各類對話框的應用。這個實例中具有以下這幾種QT的標準對話框:

  • 標準文件對話框(QFileDialog)
  • 標準顏色對話框(QColorDialog)
  • 標準字體對話框(QFontDialog)
  • 標準輸入對話框(QInputDialog)
  • 標準消息對話框(QMessageDialog)

各種基本對話框都是通過調用各自不同的靜態函數來完成其功能。具體如下:

相關類 類說明 靜態函數 函數說明
QFileDialog類 標準文件對話框 getOpenFileName 獲得用戶選擇的文件名
getSaveFileName 獲得用戶保存的文件名
getExistingDirectory 獲得用戶選擇的已存在的目錄名
getOpenFileNames 獲得用戶選擇的文件名列表
QColorDialog類 標準顏色對話框 getColor 獲得用戶選擇的顏色值
QFontDialog類 標準字體對話框 getFont 獲得用戶選擇的字體
QInputDialog類 標準輸入對話框 getText 標準字符串輸入對話框
getItem 下拉表條目輸入框
getInt int類型數據輸入對話框
getDouble double類型數據輸入對話框
QMessageBox類 消息對話框 QMessageBox::question question消息框
QMessageBox::information information消息框
QMessageBox::warning warning消息框
QMessageBox::critical critical消息框
QMessageBox::about about消息框
QMessageBox::aboutQt about Qt消息框

各個靜態函數詳細說明

  • 標準文件對話框QFileDialog::getOpenFileName()
    static QString getOpenFileName(QWidget *parent = nullptr,	//標準文件對話框的父窗口
                                   const QString &caption = QString(),	//標準文件對話框的標題名
                                   const QString &dir = QString(),	//指定默認目錄
                                   const QString &filter = QString(),//制定對文件類型的過濾
                                   QString *selectedFilter = nullptr,//用戶選擇的過濾器通過此函數返回
                                   Options options = Options());//選擇顯示文件名的個數,默認是同時是目錄與文件名

void Dialog::showFile()
{
    //設置標準文件對話框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}

在這裏插入圖片描述

  • 標準顏色對話框QColor::getColor()
    static QColor getColor(const QColor &initial = Qt::white,	//默認選中的顏色
                           QWidget *parent = nullptr,	//對話框的父窗口
                           const QString &title = QString(),	//對話框的標題名
                           ColorDialogOptions options = ColorDialogOptions());

void Dialog::showColor()
{
    //設置標準顏色對話框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

在這裏插入圖片描述

  • 標準字體對話框QFontDialog::getFont()
    static QFont getFont(bool *ok, //若用戶選擇“ok”則該參數爲true,函數返回所選擇的字體。否則設爲false,返回默認字體。
    					QWidget *parent = nullptr);//父窗口

void Dialog::showFont()
{
    //設置標準字體對話框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}

在這裏插入圖片描述

  • 標準輸入對話框類:標準字符串輸入對話框QInputDialog::getText()
    static QString getText(QWidget *parent, //父窗口
    						const QString &title, //標題名
    						const QString &label,//標籤提示
                           QLineEdit::EchoMode echo = QLineEdit::Normal,//指定輸入對話框中的QLineEdit控件輸入模式
                           const QString &text = QString(), //制定QLineEdit控件出現的文字
                           bool *ok = nullptr,//用戶選中“ok”則返回true,否則爲false
                           Qt::WindowFlags flags = Qt::WindowFlags(),
                           Qt::InputMethodHints inputMethodHints = Qt::ImhNone);

void InputDlg::ChangeName()
{
    //標準字符串輸入對話框
    bool ok;
    QString text = QInputDialog::getText(this, tr("標準字符串輸入對話框"), tr("請輸入姓名:"), QLineEdit::Normal, nameLabel2->text(), &ok);
    if(ok && !text.isEmpty())
    {
        nameLabel2->setText(text);
    }

}

在這裏插入圖片描述

  • 標準輸入對話框類:標準條目選擇對話框QInputDialog::getItem()
    static QString getItem(QWidget *parent, //父窗口
    						const QString &title, //對話框標題名
    						const QString &label,//標籤提示
                           const QStringList &items, //指定控件顯示的可選條目爲一個QStringList對象
                           int current = 0, //對話框彈出時默認顯示的條目序號。
                           bool editable = true,//指定控件中顯示的文字是否可編輯
                           bool *ok = nullptr, //提示按鈕觸發,如果是ok按鈕則爲true,否則爲false。
                           Qt::WindowFlags flags = Qt::WindowFlags(),//指明窗口標識。
                           Qt::InputMethodHints inputMethodHints = Qt::ImhNone);

void InputDlg::ChangeSex()
{
    //標準條目選擇對話框
    QStringList SexItems;
    SexItems << tr("男") << tr("女");
    bool ok;
    QString SexItem = QInputDialog::getItem(this, tr("標準條目選擇對話框"), tr("請選擇性別:"), SexItems, 0, false, &ok);
    if(ok && !SexItem.isEmpty())
    {
        sexLabel2->setText(SexItem);
    }
}

在這裏插入圖片描述

  • 標準輸入對話框類:標準int類型輸入對話框QInputDialog::getInt()
    static int getInt(QWidget *parent, //父窗口
    					const QString &title, 標題
    					const QString &label, 標籤提示
    					int value = 0,//默認顯示值
                      int minValue = -2147483647, //數值範圍最小值
                      int maxValue = 2147483647,//數值範圍最大值
                      int step = 1, //指定控件步進值
                      bool *ok = nullptr, //提示按鈕觸發情況反饋
                      Qt::WindowFlags flags = Qt::WindowFlags());

void InputDlg::ChangeAge()
{
    //標準int類型輸入對話框
    bool ok;
    int age = QInputDialog::getInt(this, tr("標準int類型輸入對話框"), tr("請輸入年齡:"), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);
    if(ok)
    {
        ageLabel2->setText(QString(tr("%1")).arg(age));
    }
}

在這裏插入圖片描述

  • 標準輸入對話框類:標準double類型輸入對話框QInputDialog::getDouble()
    static double getDouble(QWidget *parent, 
    						const QString &title, 
    						const QString &label, 
    						double value = 0,
                            double minValue = -2147483647, 
                            double maxValue = 2147483647,
                            int decimals = 1, 
                            bool *ok = nullptr, 
                            Qt::WindowFlags flags = Qt::WindowFlags());
                            
void InputDlg::ChangeScore()
{
    //標準double類型輸入對話框
    bool ok;
    double score = QInputDialog::getDouble(this, tr("標準double類型輸入對話框"), tr("請輸入成績:"), scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
    if(ok)
    {
        scoreLabel2->setText(QString(tr("%1")).arg(score));
    }
}

在這裏插入圖片描述

  • 消息對話框:question消息框QMessageBox::question()
    static int question(QWidget *parent, 
    					const QString &title,
                        const QString& text,
                        int button0, 
                        int button1 = 0, 
                        int button2 = 0);

void MsgBoxDlg::showQuestionMsg()
{
    //question消息對話框
    label->setText(tr("question message box"));
    switch (QMessageBox::question(this, tr("question消息框"), tr("您現在已經修改完成,是否結束程序"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)) {
    case QMessageBox::Ok:
        label->setText("Question button/OK");
        break;
    case QMessageBox::Cancel:
        label->setText("Question button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

在這裏插入圖片描述

  • 消息對話框:information消息框QMessageBox::information()
    static int information(QWidget *parent, 
    						const QString &title,
                           const QString& text,
                           int button0, 
                           int button1 = 0, 
                           int button2 = 0);

void MsgBoxDlg::showInformationMsg()
{
    //information消息框
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this, tr("information消息框"), tr("這個information消息框測試,歡迎您!"));
    return ;
}

在這裏插入圖片描述

  • 消息對話框:warning消息框QMessageBox::warning()
    static int warning(QWidget *parent, 
    					const QString &title,
                       const QString& text,
                       int button0, 
                       int button1, 
                       int button2 = 0);

void MsgBoxDlg::showWarningMsg()
{
    //warning消息框
    label->setText(tr("warning message box"));
    switch (QMessageBox::warning(this, tr("warning消息框"), tr("您修改的內容還未保存,是否要保存對文檔的修改?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save)) {
    case QMessageBox::Save:
        label->setText("Warning button/Save");
        break;
    case QMessageBox::Discard:
        label->setText("Warning button/Discard");
        break;
    case QMessageBox::Cancel:
        label->setText("Warning button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

在這裏插入圖片描述

  • 消息對話框:critical消息框QMessageBox::critical()
    static int critical(QWidget *parent, 
    					const QString &title,
                        const QString& text,
                        int button0, 
                        int button1, 
                        int button2 = 0);


void MsgBoxDlg::showCriticalMsg()
{
    //critical消息框
    label->setText(tr("critical message box"));
    QMessageBox::critical(this, tr("critical消息框"), tr("這是一個critical消息框測試!"));
    return ;
}

在這裏插入圖片描述

  • 消息對話框:about消息框QMessageBox::about()
    static void about(QWidget *parent, //父窗口
    					const QString &title, //對話框標題
    					const QString &text);//標籤提示字符串

void MsgBoxDlg::showAboutMsg()
{
    //about消息框
    label->setText(tr("about message box"));
    QMessageBox::about(this, tr("about消息框"), tr("這是一個about消息框測試!"));
    return ;
}

在這裏插入圖片描述

  • 消息對話框:aboutQt消息框QMessageBox::aboutQt()
    static void aboutQt(QWidget *parent, 
    					const QString &title = QString());

void MsgBoxDlg::showAboutQtMsg()
{
    //about Qt消息框
    label->setText(tr("about message box"));
    QMessageBox::aboutQt(this, tr("about Qt消息框"));
    return ;
}

在這裏插入圖片描述

實例實現

新建Qt項目“DialogExample”,基類選擇“QDialog”,類名不變,取消“創建界面”複選框。

  • dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QMessageBox>

#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFrame>
#include "inputdlg.h"
#include "msgboxdlg.h"


class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    //標準文件對話框槽函數
    void showFile();
    //標準顏色對話框槽函數
    void showColor();
    //標準字體對話框槽函數
    void showFont();
    //標準輸入對話框槽函數
    void showInputDlg();
    //各種消息對話框槽函數
    void showMsgDlg();
    //自定義消息對話框槽函數
    void showCustomDlg();

private:
    //主體佈局
    QGridLayout *mainLayout;

    //標準文件對話框
    QPushButton *fileBtn;
    QLineEdit *fileLineEdit;

    //標準顏色對話框
    QPushButton *colorBtn;
    QFrame *colorFrame;

    //標準字體對話框
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;

    //標準輸入對話框
    InputDlg *inputDlg;
    QPushButton *inputBtn;

    //各種消息對話框
    QPushButton *MsgBtn;
    MsgBoxDlg *msgDlg;

    //自定義消息框
    QPushButton *CustomBtn;
    QLabel *label;


};
#endif // DIALOG_H

  • dialog.cpp
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //設置窗口標題
    setWindowTitle(tr("各種標準對話框實例"));

    //標準文件對話框
    fileBtn = new QPushButton(tr("文件標準對話框實例"));
    fileLineEdit = new QLineEdit;

    //標準延時對話框
    colorBtn = new QPushButton(tr("標準顏色對話框實例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    //改變背景顏色
    colorFrame->setAutoFillBackground(true);

    //標準字體對話框
    fontBtn = new QPushButton(tr("標準字體對話框實例"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));

    //標準輸入對話框
    inputBtn = new QPushButton(tr("標準輸入對話框實例"));

    //各種消息對話框
    MsgBtn = new QPushButton(tr("標準消息對話框實例"));

    //自定義消息對話框
    CustomBtn = new QPushButton(tr("自定義消息對話框實例"));
    label = new QLabel;
    label->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    //主體佈局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);

    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);

    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);

    mainLayout->addWidget(inputBtn, 3, 0);
    mainLayout->addWidget(MsgBtn, 3, 1);

    mainLayout->addWidget(CustomBtn, 4, 0);
    mainLayout->addWidget(label, 4, 1);


    //實現槽函數鏈接
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDlg()));
    connect(MsgBtn, SIGNAL(clicked()), this, SLOT(showMsgDlg()));
    connect(CustomBtn, SIGNAL(clicked()), this, SLOT(showCustomDlg()));

}

Dialog::~Dialog()
{
}

void Dialog::showFile()
{
    //設置標準文件對話框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}

void Dialog::showColor()
{
    //設置標準顏色對話框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

void Dialog::showFont()
{
    //設置標準字體對話框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}

void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}

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

void Dialog::showCustomDlg()
{
    label->setText(tr("Custom Message Box"));
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(tr("用戶自定義消息對話框"));
    QPushButton *yesBtn = customMsgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton(tr("No"), QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton(tr("Cancel"), QMessageBox::ActionRole);

    customMsgBox.setText(tr("這是一個用戶自定義消息對話框!"));
    customMsgBox.setIconPixmap(QPixmap("Qt.png"));
    customMsgBox.exec();

    if(customMsgBox.clickedButton() == yesBtn)
        label->setText(tr("Custom message Box/yes"));
    if(customMsgBox.clickedButton() == noBtn)
        label->setText(tr("Custom message Box/no"));
    if(customMsgBox.clickedButton() == cancelBtn)
        label->setText(tr("Custom message Box/cancel"));
    return ;
}

  • 添加標準輸入對話框的各項實例,添加新文件->C++類,命令爲InpuDlg,繼承自QDialog類,具體代碼如下:
  • inputdlg.h
#ifndef INPUTDLG_H
#define INPUTDLG_H

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

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

private slots:
    void ChangeName();
    void ChangeSex();
    void ChangeAge();
    void ChangeScore();
private:
    QLabel *nameLabel1;
    QLabel *sexLabel1;
    QLabel *ageLabel1;
    QLabel *scoreLabel1;
    QLabel *nameLabel2;
    QLabel *sexLabel2;
    QLabel *ageLabel2;
    QLabel *scoreLabel2;
    QPushButton *nameBtn;
    QPushButton *sexBtn;
    QPushButton *ageBtn;
    QPushButton *scoreBtn;
    QGridLayout *mainLayout;
};

#endif // INPUTDLG_H

  • inputdlg.cpp
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //設置窗口標題
    setWindowTitle(tr("各種標準對話框實例"));

    //標準文件對話框
    fileBtn = new QPushButton(tr("文件標準對話框實例"));
    fileLineEdit = new QLineEdit;

    //標準延時對話框
    colorBtn = new QPushButton(tr("標準顏色對話框實例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    //改變背景顏色
    colorFrame->setAutoFillBackground(true);

    //標準字體對話框
    fontBtn = new QPushButton(tr("標準字體對話框實例"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));

    //標準輸入對話框
    inputBtn = new QPushButton(tr("標準輸入對話框實例"));

    //各種消息對話框
    MsgBtn = new QPushButton(tr("標準消息對話框實例"));

    //自定義消息對話框
    CustomBtn = new QPushButton(tr("自定義消息對話框實例"));
    label = new QLabel;
    label->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    //主體佈局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);

    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);

    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);

    mainLayout->addWidget(inputBtn, 3, 0);
    mainLayout->addWidget(MsgBtn, 3, 1);

    mainLayout->addWidget(CustomBtn, 4, 0);
    mainLayout->addWidget(label, 4, 1);


    //實現槽函數鏈接
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDlg()));
    connect(MsgBtn, SIGNAL(clicked()), this, SLOT(showMsgDlg()));
    connect(CustomBtn, SIGNAL(clicked()), this, SLOT(showCustomDlg()));

}

Dialog::~Dialog()
{
}

void Dialog::showFile()
{
    //設置標準文件對話框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}

void Dialog::showColor()
{
    //設置標準顏色對話框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

void Dialog::showFont()
{
    //設置標準字體對話框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}

void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}

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

void Dialog::showCustomDlg()
{
    label->setText(tr("Custom Message Box"));
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(tr("用戶自定義消息對話框"));
    QPushButton *yesBtn = customMsgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton(tr("No"), QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton(tr("Cancel"), QMessageBox::ActionRole);

    customMsgBox.setText(tr("這是一個用戶自定義消息對話框!"));
    customMsgBox.setIconPixmap(QPixmap("Qt.png"));
    customMsgBox.exec();

    if(customMsgBox.clickedButton() == yesBtn)
        label->setText(tr("Custom message Box/yes"));
    if(customMsgBox.clickedButton() == noBtn)
        label->setText(tr("Custom message Box/no"));
    if(customMsgBox.clickedButton() == cancelBtn)
        label->setText(tr("Custom message Box/cancel"));
    return ;
}

  • 最後添加消息對話框類的各項示例,添加新文件->選擇C++類->命令爲MsgBoxDlg,同樣選擇繼承自QDialog,具體代碼如下:
  • msgboxdlg.h
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H

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


class MsgBoxDlg : public QDialog
{
    Q_OBJECT
public:
    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

  • msgboxdlg.cpp
#include "msgboxdlg.h"

MsgBoxDlg::MsgBoxDlg(QWidget *parent)
{
    setWindowTitle(tr("標準對話框實例"));
    label = new QLabel(tr("請選擇一種消息框"));
    questionBtn = new QPushButton(tr("QusetionMsg"));
    informationBtn = new QPushButton(tr("InformationMsg"));
    warningBtn = new QPushButton(tr("warningMsg"));
    criticalBtn = new QPushButton(tr("criticalMsg"));
    aboutBtn = new QPushButton(tr("aboutMsg"));
    aboutQtBtn = new QPushButton(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(showInformationMsg()));
    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()));

}

void MsgBoxDlg::showQuestionMsg()
{
    //question消息對話框
    label->setText(tr("question message box"));
    switch (QMessageBox::question(this, tr("question消息框"), tr("您現在已經修改完成,是否結束程序"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)) {
    case QMessageBox::Ok:
        label->setText("Question button/OK");
        break;
    case QMessageBox::Cancel:
        label->setText("Question button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

void MsgBoxDlg::showInformationMsg()
{
    //information消息框
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this, tr("information消息框"), tr("這個information消息框測試,歡迎您!"));
    return ;
}

void MsgBoxDlg::showWarningMsg()
{
    //warning消息框
    label->setText(tr("warning message box"));
    switch (QMessageBox::warning(this, tr("warning消息框"), tr("您修改的內容還未保存,是否要保存對文檔的修改?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save)) {
    case QMessageBox::Save:
        label->setText("Warning button/Save");
        break;
    case QMessageBox::Discard:
        label->setText("Warning button/Discard");
        break;
    case QMessageBox::Cancel:
        label->setText("Warning button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

void MsgBoxDlg::showCriticalMsg()
{
    //critical消息框
    label->setText(tr("critical message box"));
    QMessageBox::critical(this, tr("critical消息框"), tr("這是一個critical消息框測試!"));
    return ;
}

void MsgBoxDlg::showAboutMsg()
{
    //about消息框
    label->setText(tr("about message box"));
    QMessageBox::about(this, tr("about消息框"), tr("這是一個about消息框測試!"));
    return ;
}

void MsgBoxDlg::showAboutQtMsg()
{
    //about Qt消息框
    label->setText(tr("about message box"));
    QMessageBox::aboutQt(this, tr("about Qt消息框"));
    return ;
}

  • 最終實現:
    主界面
    在這裏插入圖片描述
    標準輸入對話框界面
    在這裏插入圖片描述標準消息對話框界面
    在這裏插入圖片描述自定義消息對話框
    在這裏插入圖片描述

工具盒類

工具盒類又稱爲QToolBox,QToolBox提供了一種列狀的層疊窗體,並且提供了一種快速訪問命令或選擇項的按鈕,通常在工具條中使用。
抽屜效果是軟件界面設計中的一種常用形式,可以以一種動態直觀的方式在大小有限的界面上擴展出更多的功能。

示例“qq界面實例”

通過實現類似QQ抽屜效果的實例來介紹QToolBox類的使用。

實現步驟

  • 新建Qt widget application,項目名稱爲“MyQQExample”,基類選擇“QDialog”,取消“創建界面”複選框選擇。
  • 在項目上點右鍵添加新文件,選擇“C++Class”選項,基類選擇‘QToolBox’,輸入類名稱爲“Drawer”。
  • 在drawer.h中添加以下代碼
#ifndef DRAWER_H
#define DRAWER_H

#include <QObject>
#include <QToolBox>
#include <QToolButton>
#include <QGroupBox>
#include <QVBoxLayout>

class Drawer : public QToolBox
{
public:
    Drawer(QWidget *parent = 0, Qt::WindowFlags f = 0);

private:
    QToolButton *toolBtn1_1;
    QToolButton *toolBtn1_2;
    QToolButton *toolBtn1_3;
    QToolButton *toolBtn1_4;
    QToolButton *toolBtn1_5;

    QToolButton *toolBtn2_1;
    QToolButton *toolBtn2_2;

    QToolButton *toolBtn3_1;
    QToolButton *toolBtn3_2;

};

#endif // DRAWER_H

  • 在drawer.cpp中添加以下代碼
#include "drawer.h"

Drawer::Drawer(QWidget *parent, Qt::WindowFlags f)
{
    //設置主窗口標題
    setWindowTitle(tr("My QQ"));

    toolBtn1_1 = new QToolButton;
    toolBtn1_1->setText(tr("張三"));
    toolBtn1_1->setIcon(QPixmap("11.png"));
    toolBtn1_1->setIconSize(QPixmap("11.png").size());
    toolBtn1_1->setAutoRaise(true);
    toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn1_2 = new QToolButton;
    toolBtn1_2->setText(tr("李四"));
    toolBtn1_2->setIcon(QPixmap("12.png"));
    toolBtn1_2->setIconSize(QPixmap("12.png").size());
    toolBtn1_2->setAutoRaise(true);
    toolBtn1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn1_3 = new QToolButton;
    toolBtn1_3->setText(tr("王五"));
    toolBtn1_3->setIcon(QPixmap("13.png"));
    toolBtn1_3->setIconSize(QPixmap("13.png").size());
    toolBtn1_3->setAutoRaise(true);
    toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn1_4 = new QToolButton;
    toolBtn1_4->setText(tr("小孫"));
    toolBtn1_4->setIcon(QPixmap("14.png"));
    toolBtn1_4->setIconSize(QPixmap("14.png").size());
    toolBtn1_4->setAutoRaise(true);
    toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn1_5 = new QToolButton;
    toolBtn1_5->setText(tr("小趙"));
    toolBtn1_5->setIcon(QPixmap("15.png"));
    toolBtn1_5->setIconSize(QPixmap("15.png").size());
    toolBtn1_5->setAutoRaise(true);
    toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    QGroupBox *groupBox1 = new QGroupBox;
    QVBoxLayout *layout1 = new QVBoxLayout(groupBox1);

    //佈局中各窗體的顯示間距
    layout1->setMargin(10);
    //佈局中各窗體的顯示位置,居中
    layout1->setAlignment(Qt::AlignCenter);
    //插入各個按鈕
    layout1->addWidget(toolBtn1_1);
    layout1->addWidget(toolBtn1_2);
    layout1->addWidget(toolBtn1_3);
    layout1->addWidget(toolBtn1_4);
    layout1->addWidget(toolBtn1_5);

    //插入一個佔位符
    layout1->addStretch();

    toolBtn2_1 = new QToolButton;
    toolBtn2_1->setText(tr("小王"));
    toolBtn2_1->setIcon(QPixmap("21.png"));
    toolBtn2_1->setIconSize(QPixmap("21.png").size());
    toolBtn2_1->setAutoRaise(true);
    toolBtn2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn2_2 = new QToolButton;
    toolBtn2_2->setText(tr("小張"));
    toolBtn2_2->setIcon(QPixmap("22.png"));
    toolBtn2_2->setIconSize(QPixmap("22.png").size());
    toolBtn2_2->setAutoRaise(true);
    toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    QGroupBox *groupBox2 = new QGroupBox;
    QVBoxLayout *layout2 = new QVBoxLayout(groupBox2);
    layout2->setMargin(10);
    layout2->setAlignment(Qt::AlignCenter);
    layout2->addWidget(toolBtn2_1);
    layout2->addWidget(toolBtn2_2);

    toolBtn3_1 = new QToolButton;
    toolBtn3_1->setText(tr("小陳"));
    toolBtn3_1->setIcon(QPixmap("31.png"));
    toolBtn3_1->setIconSize(QPixmap("31.png").size());
    toolBtn3_1->setAutoRaise(true);
    toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    toolBtn3_1 = new QToolButton;
    toolBtn3_1->setText(tr("小李"));
    toolBtn3_1->setIcon(QPixmap("32.png"));
    toolBtn3_1->setIconSize(QPixmap("32.png").size());
    toolBtn3_1->setAutoRaise(true);
    toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    QGroupBox *groupBox3 = new QGroupBox;
    QVBoxLayout *layout3 = new QVBoxLayout(groupBox3);
    layout3->setMargin(10);
    layout3->setAlignment(Qt::AlignCenter);
    layout3->addWidget(toolBtn3_1);
    layout3->addWidget(toolBtn3_2);

    //將準備好的抽屜插入Toolbox中
    this->addItem((QWidget *)groupBox1, tr("我的好友"));
    this->addItem((QWidget *)groupBox2, tr("陌生人"));
    this->addItem((QWidget *)groupBox3, tr("黑名單"));

}

  • 修改dialog.cpp中代碼
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    drawer = new Drawer(this);
    drawer->show();
}

Dialog::~Dialog()
{
}


  • dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "drawer.h"
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

    Drawer *drawer;
};
#endif // DIALOG_H

界面效果

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

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