Qt5——基本對話框(2)


進度條

通常在處理長時間任務時需要提供進度條用於顯示時間。
進度條對話框的使用方法有兩種,即模態方式與非模態方式。
模態方式的使用比較簡單方便,但必須使用QApplication::processEvents()使事件循環保持正常進行狀態,從而確保應用不會阻塞。若使用非模態方式,則需要通過QTimer實現定時設置進度條的值。
Qt 提供了兩種顯示進度條的方式:

  • QProgessBar,提供了一種橫向或縱向顯示進度的控件表示方式,用來描述任務的完成情況;
  • QProgessDialog,提供了一種針對慢速過程的進度條對話框表示方式,用於描述任務完成的進度情況。

示例

使用一個進度條示例來進行進度條的演示

步驟

  • 新建Qt Widgets Application項目,項目名稱爲“Progress”,基類選擇“QDialog”,類名命名爲“ProgressDlg”,取消“創建界面”複選框。
  • 在progressdlg.h中添加以下代碼
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QProgressBar>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>
#include <QLineEdit>

class Dialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void  startProgress();

private:
    QLabel *FileNum;
    QLineEdit *FileNumLineEdit;
    QLabel *ProgressType;
    QComboBox *comBox;
    QProgressBar *progressBar;
    QPushButton *startBtn;
    QGridLayout *mainLayout;
};
#endif // DIALOG_H

  • progressdlg.cpp文件中內容如下:
#include "dialog.h"
#include <QProgressDialog>
#include <QFont>
#include <QDebug>
#include <QThread>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    QFont font("ZYSong18030", 12);
    setFont(font);
    setWindowTitle(tr("Progress"));

    FileNum = new QLabel(tr("文件數目:"));
    FileNumLineEdit = new QLineEdit;
    FileNumLineEdit->setText(tr("10000"));
    ProgressType = new QLabel(tr("顯示類型:"));
    comBox = new QComboBox;
    comBox->addItem(tr("progressBar"));
    comBox->addItem(tr("ProgressDialog"));
    progressBar = new QProgressBar;
    startBtn = new QPushButton(tr("開始"));

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(FileNum, 0, 0);
    mainLayout->addWidget(FileNumLineEdit, 0 ,1);
    mainLayout->addWidget(ProgressType, 1, 0);
    mainLayout->addWidget(comBox, 1, 1);
    mainLayout->addWidget(progressBar, 2, 0, 1, 2);
    mainLayout->addWidget(startBtn, 3, 1);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);

    connect(startBtn, SIGNAL(clicked()), this, SLOT(startProgress()));

}

Dialog::~Dialog()
{
}

void Dialog::startProgress()
{
    bool ok;
    int num = FileNumLineEdit->text().toInt(&ok);
    if(comBox->currentIndex() == 0)
    {
        progressBar->setRange(0, num);
        for(int i = 0; i <= num; ++i)
        {
            progressBar->setValue(i);
        }
    }
    else if(comBox->currentIndex() == 1)
    {
        //創建一個進度對話框
        QProgressDialog *progressDialog = new QProgressDialog(this);

        qDebug() << "QProgressDialog display" << num;
        QFont font("ZYSong1830", 12);
        progressDialog->setFont(font);
        progressDialog->setWindowModality(Qt::WindowModal);
        progressDialog->setMinimumDuration(5);
        progressDialog->setWindowTitle(tr("please Wait!"));
        progressDialog->setLabelText(tr("Copying..."));
        progressDialog->setCancelButtonText(tr("Cancel"));
        progressDialog->setRange(0, num);
//        progressDialog->show();
        progressDialog->update();
        for(int i = 0; i < num + 1; i++)
        {
            QThread::sleep(1);
            progressDialog->setValue(i);
            progressDialog->update();
            if(progressDialog->wasCanceled())
            {
                return ;
            }
        }
    }
}

實現效果

  • progressbar效果
    在這裏插入圖片描述
  • progressdialog效果
    在這裏插入圖片描述

調色板

在實際應用中,經常需要改變某個控件的顏色外觀,如背景、文字顏色等。Qt提供的調色板類QPalette 專門用於管理對話框的的外觀顯示。
QPalette類相當於對話框或控件的調色板,它管理着控件或窗體的所有顏色信息。。每個窗口或控件都包含一個QPalette對象,在顯示時,按照它的QPalette對象中對各部分各狀態下的顏色的描述進行繪製。

示例實現

實現步驟

通過實例實現窗體調色板改變控件各部分顏色。

  • 新建Qt Widget Application,項目名稱命名爲“Palette”,基類選擇“QDialog”,類名命名爲“Palette”,取消“創建界面”複選框,完成項目創建。
  • palette.h
#ifndef PALETE_H
#define PALETE_H

#include <QDialog>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>

class Palete : public QDialog
{
    Q_OBJECT

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

    //完成窗體左半部分顏色選擇區域
    void createCtrlFrame();
    //完成窗體右半部分的創建
    void createContentFrame();
    //完成顏色下拉列表框中插入顏色的創建
    void fillColorList(QComboBox *);

private slots:
    void showWindow();
    void showWindowText();
    void showButton();
    void showButtonText();
    void showBase();

private:
    //顏色選擇面板
    QFrame *ctrlFrame;

    QLabel *windowLabel;
    QComboBox *windowComboBox;
    QLabel *windowTextLabel;
    QComboBox *windowTextComboBox;
    QLabel *buttonLabel;
    QComboBox *buttonComboBox;
    QLabel *buttonTextLabel;
    QComboBox *buttonTextComboBox;
    QLabel *baseLabel;
    QComboBox *baseTextComboBox;

    QFrame *contentFrame;
    QLabel *label1;
    QComboBox *comboBox1;
    QLabel *label2;
    QLineEdit *textEdit2;
    QTextEdit *textEdit;
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
};
#endif // PALETE_H

  • palette.cpp
#include "palete.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>

Palete::Palete(QWidget *parent)
    : QDialog(parent)
{
    createCtrlFrame();
    createContentFrame();
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(ctrlFrame);
    mainLayout->addWidget(contentFrame);
}

Palete::~Palete()
{
}

void Palete::createCtrlFrame()
{
    //顏色選擇面板
    ctrlFrame = new QFrame;
    windowLabel = new QLabel(tr("QPalette::Window:"));
    windowComboBox = new QComboBox;
    fillColorList(windowComboBox);
    connect(windowComboBox, SIGNAL(activated(int)), this, SLOT(showWindow()));

    windowTextLabel = new QLabel(tr("QPalette::WindowText:"));
    windowTextComboBox = new QComboBox;
    fillColorList(windowTextComboBox);
    connect(windowTextComboBox, SIGNAL(activated(int)), this, SLOT(showWindowText()));

    buttonLabel = new QLabel(tr("QPalette::button:"));
    buttonComboBox = new QComboBox;
    fillColorList(buttonComboBox);
    connect(buttonComboBox, SIGNAL(activated(int)), this, SLOT(showButton()));

    buttonTextLabel = new QLabel(tr("QPalette::buttonText:"));
    buttonTextComboBox = new QComboBox;
    fillColorList(buttonTextComboBox);
    connect(buttonTextComboBox, SIGNAL(activated(int)), this, SLOT(showButtonText()));

    baseLabel = new QLabel(tr("QPalette::Base:"));
    baseTextComboBox = new QComboBox;
    fillColorList(baseTextComboBox);
    connect(baseTextComboBox, SIGNAL(activated(int)), this, SLOT(showBase()));

    QGridLayout *mainLayout = new QGridLayout(ctrlFrame);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(windowLabel, 0, 0);
    mainLayout->addWidget(windowComboBox, 0, 1);
    mainLayout->addWidget(windowTextLabel, 1, 0);
    mainLayout->addWidget(windowTextComboBox, 1, 1);
    mainLayout->addWidget(buttonLabel, 2, 0);
    mainLayout->addWidget(buttonComboBox, 2, 1);
    mainLayout->addWidget(buttonTextLabel, 3, 0);
    mainLayout->addWidget(buttonTextComboBox, 3, 1);
    mainLayout->addWidget(baseLabel, 4, 0);
    mainLayout->addWidget(baseTextComboBox, 4, 1);

}

void Palete::createContentFrame()
{
    contentFrame = new QFrame;
    label1 = new QLabel(tr("請選擇一個值:"));
    comboBox1 = new QComboBox;
    label2 = new QLabel(tr("請輸入字符串:"));
    textEdit2 = new QLineEdit;
    textEdit = new QTextEdit;
    QGridLayout *TopLayout = new QGridLayout;
    TopLayout->addWidget(label1, 0, 0);
    TopLayout->addWidget(comboBox1, 0, 1);
    TopLayout->addWidget(label2, 1, 0);
    TopLayout->addWidget(textEdit2, 1, 1);
    TopLayout->addWidget(textEdit, 2, 0, 1, 2);

    OkBtn = new QPushButton(tr("確認"));
    CancelBtn = new QPushButton(tr("取消"));

    QHBoxLayout *BottomLayout = new QHBoxLayout;
    BottomLayout->addStretch(1);
    BottomLayout->addWidget(OkBtn);
    BottomLayout->addWidget(CancelBtn);

    QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
    mainLayout->addLayout(TopLayout);
    mainLayout->addLayout(BottomLayout);
}

void Palete::showWindow()
{
    //獲取當前選擇的顏色值
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window, color);
    //把修改後的調色板信息應用到contentframe窗體中,更新顯示
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palete::showWindowText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = colorList[windowTextComboBox->currentIndex()];
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::WindowText, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palete::showButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Button, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palete::showButtonText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::ButtonText, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palete::showBase()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[baseTextComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Base, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palete::fillColorList(QComboBox *comboBox)
{
    QStringList colorlist = QColor::colorNames();
    QString color;
    foreach(color, colorlist)
    {
        QPixmap pix(QSize(70, 20));
        pix.fill(QColor(color));
        comboBox->addItem(QIcon(pix), NULL);
        comboBox->setIconSize(QSize(70, 20));
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    }
}

實現界面顯示

  • 主界面顯示
    在這裏插入圖片描述

  • 改變顏色
    在這裏插入圖片描述

  • 選擇顏色
    在這裏插入圖片描述

擴展對話框

可擴展對話框的基本實現是理由setSizeConstraint(QLayout::SetFixedSize)方法,使對話框尺寸保持相對固定。其中,最關鍵的部分是以下兩點:

  • 在整個對話框的構造函數中調用以下語句。保證了對話框尺寸相對固定。對話框尺寸根據需要顯示的控件進行拓展調整。

    layout->setSizeConstraint(QLayout::SetFixedSize);
  • 切換按鈕的實現。整個窗體可擴展的工作都在此按鈕連接的槽函數中完成。

示例實現

示例實現步驟

  • 新建Qt Widgets Application項目,命名“ExtensionDlg”,基類選擇“QDialog”,取消“創建界面”複選框。
  • 實現 exitensiondlg.h 內容如下
#ifndef EXTENSIONDLG_H
#define EXTENSIONDLG_H

#include <QDialog>

class ExtensionDlg : public QDialog
{
    Q_OBJECT

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

private slots:
    void showDetailInfo();

private:
    void createBaseInfo();//實現基本對話框窗體部分
    void createDetailInfo();//實現擴展窗體部分
    QWidget *baseWidget; //基本對話窗體部分
    QWidget *detailWidget; //擴展窗體部分
};
#endif // EXTENSIONDLG_H

  • exitensiondlg.cpp內容如下:
#include "extensiondlg.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>

ExtensionDlg::ExtensionDlg(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Extension Dialog"));
    createBaseInfo();
    createDetailInfo();
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(baseWidget);
    layout->addWidget(detailWidget);
    layout->setSizeConstraint(QLayout::SetFixedSize);
    layout->setSpacing(10);
}

ExtensionDlg::~ExtensionDlg()
{
}

void ExtensionDlg::createBaseInfo()
{
    baseWidget = new QWidget;
    QLabel *nameLabel = new QLabel(tr("姓名:"));
    QLineEdit *nameLineEdit = new QLineEdit;
    QLabel *sexLabel = new QLabel(tr("性別:"));
    QComboBox *sexCombox = new QComboBox;
    sexCombox->insertItem(0, tr("女"));
    sexCombox->insertItem(1, tr("男"));

    QGridLayout *leftlayout = new QGridLayout;
    leftlayout->addWidget(nameLabel, 0, 0);
    leftlayout->addWidget(nameLineEdit, 0, 1);
    leftlayout->addWidget(sexLabel);
    leftlayout->addWidget(sexCombox);

    QPushButton *Okbtn = new QPushButton(tr("確定"));
    QPushButton *Detailbtn = new QPushButton(tr("詳細"));
    QDialogButtonBox *btnBox = new QDialogButtonBox(Qt::Vertical);
    btnBox->addButton(Okbtn, QDialogButtonBox::ActionRole);
    btnBox->addButton(Detailbtn, QDialogButtonBox::ActionRole);
    QHBoxLayout *mainLayout = new QHBoxLayout(baseWidget);
    mainLayout->addLayout(leftlayout);
    mainLayout->addWidget(btnBox);
    connect(Detailbtn, SIGNAL(clicked()), this, SLOT(showDetailInfo()));
}

void ExtensionDlg::createDetailInfo()
{
    detailWidget = new QWidget;
    QLabel *ageLabel = new QLabel(tr("年齡:"));
    QLineEdit *ageLineEdit = new QLineEdit;
    ageLineEdit->setText(tr("30"));

    QLabel *departmentLabel = new QLabel(tr("部門"));
    QComboBox *departmentCombox = new QComboBox;
    departmentCombox->addItem(tr("部門1"));
    departmentCombox->addItem(tr("部門2"));
    departmentCombox->addItem(tr("部門3"));
    departmentCombox->addItem(tr("部門4"));
    QLabel *emailLabel = new QLabel(tr("email:"));
    QLineEdit *emailLineEdit = new QLineEdit;

    QGridLayout *mainLayout = new QGridLayout(detailWidget);
    mainLayout->addWidget(ageLabel, 0, 0);
    mainLayout->addWidget(ageLineEdit, 0 ,1);
    mainLayout->addWidget(departmentLabel, 1, 0);
    mainLayout->addWidget(departmentCombox, 1, 1);
    mainLayout->addWidget(emailLabel, 2, 0);
    mainLayout->addWidget(emailLineEdit, 2, 1);

    detailWidget->hide();
}

void ExtensionDlg::showDetailInfo()
{
    if(detailWidget->isHidden())
        detailWidget->show();
    else
        detailWidget->hide();
}

示例實現

  • 主界面一:
    在這裏插入圖片描述
  • 擴展界面

在這裏插入圖片描述

程序啓動畫面(QSplashScreen)

如果需要在程序啓動時優先顯示一個啓動或者進入程序的界面顯示,可以使用Qt中提供的QSplashScreen類實現在程序啓動過程中顯示啓動畫面的功能。

示例

實現一個程序中添加一個啓動畫面作爲啓動界面的示例:

示例步驟

  • 新建Qt Widgets Application,項目名稱爲“SplashSreen”,基類選擇“QMainWindow”,類名命名爲“MainWindow”,取消“創建界面”複選框的狀態。
  • mainWindow類中頭文件中添加:
#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>

namespace Ui {
class mainWidget;
}

class mainWidget : public QWidget
{
    Q_OBJECT

public:
    explicit mainWidget(QWidget *parent = nullptr);
    ~mainWidget();

private:
    Ui::mainWidget *ui;
};

#endif // MAINWIDGET_H

  • mainWindow類中源文件中添加:
#include "mainwidget.h"
#include "ui_mainwidget.h"

mainWidget::mainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainWidget)
{
    setWindowTitle(tr("光子通訊編解碼性能測試及分析軟件"));
    ui->setupUi(this);
    setAutoFillBackground(true);
    QPalette pal = this->palette();
    pal.setBrush(backgroundRole(), QPixmap("22.jpg"));
    setPalette(pal);

    ui->textEdit->append(tr("單光子通訊發送服務端異常,請檢查設置!"));


}

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

  • 修改main.cpp主代碼內容爲:
#include "mainwidget.h"
#include <QApplication>
#include <QIcon>
#include <QPixmap>
#include <QSplashScreen>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strPath = QApplication::applicationDirPath();
    strPath += "/31.jpg";
    a.setWindowIcon(QIcon("31.jpg"));

    QPixmap pixmap("23.png");
    QSplashScreen splash(pixmap);
    splash.resize(pixmap.size());
    splash.show();
    a.processEvents();


    mainWidget w;
    w.setWindowTitle(QObject::tr("main主界面"));
    w.show();

    return a.exec();
}


示例實現

在這裏插入圖片描述

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