Qt实现进度条(QProgressBar)

版权声明:本文为博主原创文章,如有需要,可以转载,但请注明出处。 https://blog.csdn.net/xunye_dream/article/details/84699683

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、显示结果

猜你喜欢

转载自blog.csdn.net/xunye_dream/article/details/84699683