欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt开发——QProgressBar/QProgressDialog进度条

程序员文章站 2024-02-25 18:18:09
...

目录

QProgressBar效果图:

QProgressDialog效果图:

进度条模态方式+模拟文件复制

progressdlg.h

progressdlg.cpp


QProgressBar效果图:

Qt开发——QProgressBar/QProgressDialog进度条

QProgressDialog效果图:

Qt开发——QProgressBar/QProgressDialog进度条

进度条模态方式+模拟文件复制

progressdlg.h

#ifndef PROGRESSDLG_H
#define PROGRESSDLG_H

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

class ProgressDlg : public QDialog
{
    Q_OBJECT

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

private:
   QLabel *FileNum;
   QLineEdit *FileNumLineEdit;
   QLabel *ProgressType;
   QComboBox *comboBox;
   QProgressBar *progressBar;
   QPushButton *startBtn;
   QGridLayout *mainLayout;

private slots:
     void startProgress();

};

#endif // PROGRESSDLG_H

progressdlg.cpp

#include "progressdlg.h"

ProgressDlg::ProgressDlg(QWidget *parent)
    : QDialog(parent)
{
    QFont font("ZYSong18030",12);
    setFont(font);//设置字体
    setWindowTitle(QStringLiteral("进度条"));
    setWindowIcon(QIcon("icon.png"));//设置程序图标
    FileNum =new QLabel;
    FileNum->setText(QStringLiteral("文件数目"));
    FileNumLineEdit=new QLineEdit;
    FileNumLineEdit->setText(QStringLiteral("100000"));
    ProgressType = new QLabel;
    ProgressType->setText(QStringLiteral("显示类型"));
    comboBox = new QComboBox;
    comboBox->addItem(QStringLiteral("progressBar"));
    comboBox->addItem(QStringLiteral("progressDialog"));
    progressBar = new QProgressBar;
    startBtn = new QPushButton();
    startBtn->setText(QStringLiteral("开始"));
    //布局设计
    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(startBtn,3,1);
    mainLayout->setMargin(25);
    mainLayout->setSpacing(10);
    //事件关联
    connect(startBtn,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);//设置步进范围
        for(int i=1;i<num+1;i++){
            progressBar->setValue(i);//模拟每个文件复制的过程
        }
    }
    else if(comboBox->currentIndex()==1){//采用进度对话框显示进度
        QProgressDialog *progressDialog=new QProgressDialog(this);//创建一个进度对话框
        QFont font("ZYSong18030",12);
        progressDialog->setFont(font);//设置字体
        progressDialog->setWindowIcon(QIcon("icon.png"));//设置程序图标
        progressDialog->setWindowModality(Qt::WindowModal);//设置进度对话框采用模态方式进行显示
        progressDialog->setMinimumDuration(3);//设置对话框出现等待时间
        progressDialog->setWindowTitle(QStringLiteral("玩命加载…"));
        progressDialog->setLabelText(QStringLiteral("复制中…"));
        progressDialog->setCancelButtonText(QStringLiteral("取消"));
        progressDialog->setRange(0,num);
        for(int i=1;i<num+1;i++){
            progressDialog->setValue(i);//模拟文件复制过程
            if(progressDialog->wasCanceled()){//检测是否取消
                return;//取消后结束程序
            }
        }
    }

}

 

相关标签: Qt开发