进度条的使用


一般来说进度条分为两种:

两种,一种是简单的进度条

另一种是进度提示框


我先把代码贴上

在testdialog.h中

#ifndef TESTDIALOG_H
#define TESTDIALOG_H

#include <QDialog>
namespace Ui {
class TestDialog;
}

class TestDialog : public QDialog
{
    Q_OBJECT

public:
    explicit TestDialog(QWidget *parent = 0);
    ~TestDialog();

private slots:

    void on_start_clicked();

    void on_up_clicked();

    void on_down_clicked();

    void on_close_clicked();
    void on_stop_clicked();

    void on_pushButton_clicked();

signals:
    void sendValue(int);//传值
private:
    Ui::TestDialog *ui;
    int i;//变量
    QTimer *timerStart;//开始定时器
    QTimer *timerUp;//加速定时器
    QTimer *timerDown;//减速定时器
};

#endif // TESTDIALOG_H

在testdialog.cpp中

#include "testdialog.h"
#include "ui_testdialog.h"
#include<QProgressDialog>
#include<QMessageBox>
#define MAXSIZE 100
#include<QTimer>
#include<windows.h>
#include<QProcess>
TestDialog::TestDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TestDialog)
{
    ui->setupUi(this);
    this->setFixedSize(width(),height());
    ui->progressBar->setValue(0);//初始值为0
    ui->progressBar->setRange(0,MAXSIZE-1);//设置进度条值的范围     //红底
    ui->progressBar->setStyleSheet("QProgressBar::chunk { background-color: rgb(255, 0, 0) }");
    timerStart=new QTimer(this);
    timerUp=new QTimer(this);
    timerDown=new QTimer(this);
    connect(timerStart,SIGNAL(timeout()),this,SLOT(on_start_clicked()));
    connect(timerUp,SIGNAL(timeout()),this,SLOT(on_up_clicked()));
    connect(timerDown,SIGNAL(timeout()),this,SLOT(on_down_clicked()));
    i=0;
    ui->start->setEnabled(true);
    ui->up->setEnabled(false);
    ui->down->setEnabled(false);
    ui->stop->setEnabled(false);
}
TestDialog::~TestDialog()
{
    delete ui;
}
void TestDialog::on_start_clicked()
{
    ui->start->setEnabled(false);
    ui->up->setEnabled(true);
    ui->down->setEnabled(true);
    ui->stop->setEnabled(true);
        timerStart->start(1);
        timerUp->stop();
        timerDown->stop();
        if(i >=MAXSIZE-1 )
        {
            ui->progressBar->setValue(MAXSIZE-1);
            emit sendValue(MAXSIZE-1);
            timerStart->stop();
            QMessageBox::information(this,tr("提示"),tr("结束!"),QMessageBox::Ok|QMessageBox::Default);
            return;
        }
                //赋值
                ui->progressBar->setValue(i);
                i+=1;
                Sleep(100);
                emit sendValue(i);
}
void TestDialog::on_up_clicked()
{
    timerUp->start(1);
    timerDown->stop();
    timerStart->stop();
    if(i >=MAXSIZE-1 )
    {
     ui->progressBar->setValue(MAXSIZE-1);
     emit sendValue(MAXSIZE-1);
     timerUp->stop();
    QMessageBox::information(this,tr("提示"),tr("结束!"),QMessageBox::Ok|QMessageBox::Default);
    return;
    }
    ui->progressBar->setValue(i);
    i+=1;
    emit sendValue(i);
}
void TestDialog::on_down_clicked()
{
    timerDown->start(1);
    timerStart->stop();
    timerUp->stop();
        if(i >=MAXSIZE-1 )
        {
            ui->progressBar->setValue(MAXSIZE-1);
            emit sendValue(MAXSIZE-1);
            timerDown->stop();
            QMessageBox::information(this,tr("提示"),tr("结束!"),QMessageBox::Ok|QMessageBox::Default);
            return;
        }
            ui->progressBar->setValue(i);
            i+=1;
            emit sendValue(i);
}
void TestDialog::on_close_clicked()
{
     this->close();
}
void TestDialog::on_stop_clicked()
{
    ui->start->setEnabled(true);
    ui->up->setEnabled(false);
    ui->down->setEnabled(false);
    ui->stop->setEnabled(false);
        timerStart->stop();
        timerDown->stop();
        timerUp->stop();
}
void TestDialog::on_pushButton_clicked()
{
    QProgressDialog dialog(tr("文件复制操作"),tr("取消"),0,99,this);
    dialog.setWindowTitle(tr("进度条对话框"));
    dialog.setWindowModality(Qt::WindowModal);
    dialog.show();
    for(int i=0;i<=MAXSIZE-1;i++)
    {
        dialog.setValue(i);
        Sleep(50);
        //QCoreApplication::processEvents();//防止界面冻结
        if(dialog.wasCanceled()) break; //取消就中断
        if(i==MAXSIZE-1)
        {
            QMessageBox::information(this,tr("系统提示"),tr("复制完毕"),QMessageBox::Ok);
        }
        }
    }

 
 
 在main.cpp中 

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值