第四章 Qt5基本对话框——实例:进度条QProgressBar

第四章 Qt5基本对话框——实例:进度条QProgressBar_第1张图片

 progressdlg.h

#ifndef PROGRESSDLG_H
#define PROGRESSDLG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class ProgressDlg : public QDialog
{
    Q_OBJECT

public:
    ProgressDlg(QWidget *parent = 0);
    ~ProgressDlg();
private slots:
    void startProgress();
private:
    QLabel * FileNum;
    QLineEdit * FileNumLineEdit;
    QLabel * ProgressType;
    QComboBox * comboBox;
    QProgressBar * ProgressBar;
    QPushButton * starBtn;
    QGridLayout * mainlayout;
};

#endif // PROGRESSDLG_H

 progressdlg.cpp

#include "progressdlg.h"

ProgressDlg::ProgressDlg(QWidget *parent)
    : QDialog(parent)
{
    QFont font("黑体",12);
    setFont(font);
    setWindowTitle(tr("Progress"));
    FileNum = new QLabel;
    FileNum->setText(tr("文件数量:"));
    FileNumLineEdit = new QLineEdit;
    FileNumLineEdit->setText(tr("10000"));
    ProgressType = new QLabel;
    ProgressType->setText(tr("显示类型:"));
    comboBox = new QComboBox;
    comboBox->addItem(tr("progressBar"));
    comboBox->addItem(tr("progressDialog"));
    ProgressBar = new QProgressBar;
    starBtn = new QPushButton;
    starBtn->setText(tr("开始"));
    mainlayout = new QGridLayout(this);
    mainlayout->addWidget(FileNum,0,0);
    mainlayout->addWidget(FileNumLineEdit,0,1);
    mainlayout->addWidget(ProgressType,1,0);
    mainlayout->addWidget(comboBox,1,1);
    mainlayout->addWidget(ProgressBar,2,0,1,2);
    mainlayout->addWidget(starBtn,3,1);
    mainlayout->setMargin(15);
    mainlayout->setSpacing(10);
    connect(starBtn,SIGNAL(clicked()),this,SLOT(startProgress()));

}

ProgressDlg::~ProgressDlg()
{

}

void ProgressDlg::startProgress()
{
    bool ok;
    int num = FileNumLineEdit->text().toInt(&ok);//获取当前需要复制的文件数目,这里对应进度条的总步进值
    if(comboBox->currentIndex() == 0){
        ProgressBar->setRange(0,num);//设置进度条的步进范围从0到需要复制的文件数目
        for(int i = 1; isetValue(i);//模拟每一个文件的复制过程,进度条总的步进值为需要复制的文件数目。
                                    //当复制完一个文件后,步进值增加一
        }
    }else if(comboBox->currentIndex() == 1){//采用进度对话框显示进度
        //创建一个进度对话框
        QProgressDialog * progressDialog = new QProgressDialog(this);
        QFont font("楷书",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);
        for(int i=1; isetValue(i);
            if(progressDialog->wasCanceled())//检测“取消”按钮是否触发,若触发则退出循环并关闭进度对话框
                return;
        }
    }
}

你可能感兴趣的:(Qt5开发及实例,qt,开发语言)