Qt实现进度条(QProgressBar)

本文详细介绍了进度条在软件开发中的作用,包括其显示方式、使用方式及如何通过QProgressBar和QProgressDialog实现进度显示。文章提供了代码示例,展示了如何在Qt中创建进度条并更新其值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、进度条的作用

    用于显示时间,并告诉用户当前任务的执行进展。

2、进度条的使用方式

    两种:模态方式和非模态方式

   模态方式:使用简单,但必须使用QApplication::processEvents()使事件循环保持正常进行状态,以保证应用不会被阻塞。

   非模态方式:需通过QTime实现定时设置进度条的值。

3、进度条的显示方式

   两种:1、QProgressBar,可以横向或纵向显示;2、QProgressDialog,以对话框的形式表示。

4、标准进度条的基本元素

   进度显示条、取消按钮、标签。

5、样例

1、头文件progressdlg.h

#ifndef PROGRESSDLG_H
#define PROGRESSDLG_H
 
#include <QDialog>
 
class QLabel;
class QLineEdit;
class QComboBox;
class QProgressBar;
class QPushButton;
class QGridLayout;
 
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

2、源文件 progressdlg.cpp

#include "progressdlg.h"
#include <QFont>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QProgressBar>
#include <QPushButton>
#include <QGridLayout>
#include <QProgressDialog>
 
ProgressDlg::ProgressDlg(QWidget *parent)
    : QDialog(parent)
{
    QFont font("ZYSong18030", 12);
    setFont(font);
    setWindowTitle(tr("Progress"));
 
    FileNum = new QLabel;
    FileNum->setText(tr("File Number: "));
    FileNumLineEdit = new QLineEdit;
    FileNumLineEdit->setText(tr("100000"));
 
    ProgressType = new QLabel;
    ProgressType->setText(tr("Dispaly Type: "));
    comboBox = new QComboBox;
    comboBox->addItem(tr("progressBar"));
    comboBox->addItem(tr("progressDialog"));
 
    progressBar = new QProgressBar;
 
    starBtn = new QPushButton;
    starBtn->setText(tr("Start"));
 
    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()
{
    if(FileNum) delete FileNum;
    if(FileNumLineEdit) delete FileNumLineEdit;
    if(ProgressType) delete ProgressType;
    if(comboBox) delete comboBox;
    if(progressBar) delete progressBar;
    if(starBtn) delete starBtn;
    if(mainLayout) delete mainLayout;
}
 
void ProgressDlg::startProgress()
{
    bool ok = false;
    int num = FileNumLineEdit->text().toInt(&ok);
 
    if (!ok)
        return ;
 
    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->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 = 0; i < num + 1; ++i)
        {
            progressDialog->setValue(i);
            if (progressDialog->wasCanceled())
            {
                delete progressDialog;
                return ;
            }
        }
 
        delete progressDialog;
    }
}

3、main.cpp

#include "progressdlg.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ProgressDlg w;
    w.show();
 
    return a.exec();
}

6、显示结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值