QT-纯代码控件-QProgressBar(进度条)

用以实现一个读取文件进度条的功能

1.新建一个无ui界面的工程,其基类为dialog对话框类

在这里插入图片描述

2.代码实现

dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>

class Dialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void startProgress();           //定义一个槽函数


private:
    QLabel * fileNum;               //控件声明
    QProgressBar * progressBar;     //控件声明
    QPushButton  * startBtn;        //控件声明
};


#endif // DIALOG_H

dialog.cpp
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    this->setWindowTitle(tr("进度条"));
    fileNum = new QLabel(this);
    fileNum->setText(tr("复制文件数目:0"));
    fileNum->setGeometry(20,10,200,18);
    progressBar = new QProgressBar(this);
    progressBar->setGeometry(20,35,200,10);
    startBtn = new QPushButton(this);
    startBtn->setText(tr("开始"));
    startBtn->setGeometry(20,55,80,30);

    //连接信号与槽
    connect(startBtn,SIGNAL(clicked()),this,SLOT(startProgress()));
}

Dialog::~Dialog()
{

}

//槽函数实现
void Dialog::startProgress()
{
    progressBar->setRange(0,100000);      //设置进度条的范围
    for(int i = 1; i <= 100000; i++)
    {
        progressBar->setValue(i);
        QString str = QString("%1").arg(i);
        str = "复制文件数目: "  +str;
        fileNum->setText(str);

    }
}

3.效果展示

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

4.不足与需改进之处

1.进程过快。效果不宜呈现,可以将进度条的值调大一点
2.后面可以引入QTimer定时器功能,以实现文件复制

5.改进之后

progressbar.h
#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H

#include <QMainWindow>
#include <QProgressBar>

class ProgressBar : public QMainWindow
{
    Q_OBJECT

public:
    ProgressBar(QWidget *parent = 0);
    ~ProgressBar();
};

//新建类
class QString;
class Progressbar : public QProgressBar
{
    Q_OBJECT
public:
    Progressbar(QWidget *parent = 0):QProgressBar(parent){}
    QString strText;

public slots:
    void stepOne();
};

#endif // PROGRESSBAR_H

progressbar.cpp
#include "progressbar.h"
#include <QString>

ProgressBar::ProgressBar(QWidget *parent)
    : QMainWindow(parent)
{
}

ProgressBar::~ProgressBar()
{

}

void Progressbar::stepOne()
{
    if(this->value()+1 <= this->maximum())
    {
        this->setValue(this->value() + 1);

        strText = "进度条 : " + this->text();
        this->setWindowTitle(strText);
    }
    else
    {
        this->setValue(this->minimum());
    }
}

main.cpp
#include "progressbar.h"
#include <QApplication>
#include <QTimer>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ProgressBar w;
    w.show();

    //进度条设置
    Progressbar * progress = new Progressbar;
    progress->setWindowTitle("进度条");
    progress->resize(400,40);
    progress->setMaximum(100);
    progress->setMinimum(0);
    progress->setValue(0);

    //设置一个定时器
    QTimer *timer = new QTimer;
    timer->start(500);
    QObject::connect(timer, SIGNAL(timeout()), progress, SLOT(stepOne()));
    progress->show();

    return a.exec();
}

6.效果展示

在这里插入图片描述

改进版本参考链接:http://blog.chinaunix.net/uid-27225886-id-3352398.html
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值