Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

lesson3:Qt 對話框完全指南——QDialog 與六大內建對話框

編輯:關於 Android 開發

一、QDialog 類別

1. 對話框的概念

對話框在各式軟體中無所不在,主要用來向使用者提示資訊或接收使用者回饋,是應用程式與使用者互動的橋樑。

對話框是一個頂層視窗,不能嵌入到其他視窗之中。

2. 對話框的兩種類型

  • 模式(Modal)對話框:開啟期間,同一應用程式的其他視窗無法被操作,必須等它關閉。顯示方式為呼叫 exec()。
  • 非模式(Modeless)對話框:開啟期間其他視窗仍可正常操作。顯示方式為呼叫 show()。

3. 繼承關係

QDialog 的父類別是 QWidget。

二、QDialog 的六大派生類

為方便開發,Qt 把一些常見功能的對話框做了封裝,提供一組標準對話框。它們多半提供靜態函數便於呼叫,且通常會採用系統本地的對話框樣式。

1. QFileDialog —— 檔案選擇

// 開啟檔案對話框,回傳選擇的檔名
QString str = QFileDialog::getOpenFileName(
    父視窗, 對話框標題, 預設路徑, 檔案過濾器);

// 依檔名開啟檔案,成功回傳 true
QFile file(str);
file.open(QIODevice::ReadWrite);

// 取得輸入流並逐行讀出
QTextStream in(&file);
in.readLine();

2. QColorDialog —— 調色盤

// 取得調色盤
QPalette palette = textEdit->palette();

// 開啟顏色對話框,取得使用者選的顏色
QColor color = QColorDialog::getColor(
    palette.color(QPalette::Text),  // 對話框初始顏色
    this                            // 父視窗
);

// 設定並套用調色盤
palette->setColor(QPalette::Text, color);
textEdit->setPalette(palette);

GUI 為不同部位分別定義了顏色標誌,可對特定區域單獨設色。

3. QFontDialog —— 字型選擇

bool ok;
QFont font = QFontDialog::getFont(&ok);
// 點「確定」時 ok 為 true,點「取消」時為 false
textEdit->setFont(font);

4. QInputDialog —— 文字輸入

QString str = QInputDialog::getText(
    this,                 // 父視窗
    "inputDialog",        // 視窗標題
    "please input",       // 輸入框上方的標籤文字
    QLineEdit::Normal,    // 編輯框顯示方式
    QDir::home(),         // 預設內容
    ok                    // 回傳的 bool 變數
);

5. QProgressDialog —— 進度條

QProgress::setRange(0, 100)  // 設定進度範圍
QProgress::setValue(50)      // 設定目前進度值

三、QMessageBox 消息框家族

Qt 提供了多種訊息框,全部屬於模態對話框,日常開發中極為常用。

  • question:帶標題與文字的詢問框,可按鈕數量與作用。
  • information:帶標題與提示文字的資訊框。
  • warning:帶標題與文字的警示框。
  • critical:帶標題與文字的嚴重錯誤框。
  • about:關於程式的訊息框。
  • aboutQt:顯示 Qt 相關資訊。

訊息框的按鈕也可以自行定制。

四、完整實例

1. 標頭檔

#ifndef BUILDINDIALOG_H
#define BUILDINDIALOG_H

#include <QtGui>

class buildInDialog : public QDialog
{
    Q_OBJECT
public:
    buildInDialog();
private:
    QPushButton *fileBtn;
    QPushButton *colorBtn;
    QPushButton *fontBtn;
    QPushButton *saveBtn;
    QPushButton *closeBtn;

    QTextEdit *textEdit;
private slots:
    void fileSlot();
    void colorSlot();
    void fontSlot();
    void saveSlot();
    void closeSlot();
};

#endif

2. 實作檔

#include "buildInDialog.h"

buildInDialog::buildInDialog()
{
    fileBtn = new QPushButton("open");
    colorBtn = new QPushButton("color");
    fontBtn = new QPushButton("font");
    saveBtn = new QPushButton("save");
    closeBtn = new QPushButton("close");

    textEdit = new QTextEdit();

    // 布局
    QVBoxLayout *vLay = new QVBoxLayout();
    QHBoxLayout *hLay = new QHBoxLayout();
    vLay->addWidget(fileBtn);
    vLay->addWidget(colorBtn);
    vLay->addWidget(fontBtn);
    vLay->addWidget(saveBtn);
    vLay->addWidget(closeBtn);

    hLay->addWidget(textEdit);
    hLay->addLayout(vLay);

    setLayout(hLay);

    connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
    connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
    connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
}

void buildInDialog::fileSlot()
{
    // 取得檔案名稱
    QString str = QFileDialog::getOpenFileName(this, "打開文件", "/", "All File(*.*)");

    // 開啟檔案
    QFile file(str);
    if(!file.open(QIODevice::ReadWrite))
        return;

    // 取得輸入流並逐行讀入
    QTextStream in(&file);
    while(!in.atEnd())
    {
        QString st = in.readLine();
        textEdit->append(st);
    }
}

void buildInDialog::colorSlot()
{
    // 取得調色盤
    QPalette palette = textEdit->palette();

    // 開啟對話框取得顏色
    QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);

    if(color.isValid())
    {
        palette.setColor(QPalette::Window, color);
        textEdit->setPalette(palette);
    }
}

void buildInDialog::fontSlot()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok)
        textEdit->setFont(font);
}

void buildInDialog::saveSlot()
{
    bool ok;
    // 取得輸入的資訊
    QString str = QInputDialog::getText(this, "輸入對話框", "請輸入名字",
                                       QLineEdit::Normal, "wj", &ok);

    // 依輸入的名字開啟檔案
    QFile file(str);
    file.open(QIODevice::WriteOnly);

    // 取得輸出流,把 textEdit 內容寫入
    QTextStream out(&file);
    out << textEdit->toPlainText() << "\n";
}

void buildInDialog::closeSlot()
{
    QProgressDialog *progress = new QProgressDialog();
    progress->setRange(0, 100);
    for(int i = 0; i <= 100; i += 10)
    {
        qApp->processEvents();
        progress->setValue(i);
        sleep(1);
    }
}

3. 主函數

#include "buildInDialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    // 設定編碼,避免中文亂碼
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));

    QApplication app(argc, argv);

    buildInDialog dialog;
    dialog.show();

    return app.exec();
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved