一般来说进度条分为两种:
两种,一种是简单的进度条
另一种是进度提示框
我先把代码贴上
在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(); }