進度條函式用法
<建置環境>建模工具為: Qt Creator 4.5.0
作業系統: window 7
編譯環境: MingW 32
<類別函式>
QProgressBar()
<目標>
<實作>
1.新建專案,本專案使用主視窗為QMainWindow
窗類別名稱設為onlywindow
2.照圖建立好自己的.ui
3.進入onlywindow.h 將要用到的函式引用incloude
再將私有變數的信號槽建好
//onlywindow.h
#ifndef ONLYWINDOW_H
#define ONLYWINDOW_H
#include <QMainWindow>
#include <QProgressBar>
#include <QPushButton>
#include <QLabel>
namespace Ui {
class onlywindow;
}
class onlywindow : public QMainWindow
{
Q_OBJECT
public:
explicit onlywindow(QWidget *parent = 0);
~onlywindow();
private slots:
void startProgress();
void resetProgress();
private:
Ui::onlywindow *ui;
};
#endif // ONLYWINDOW_H
4.進入onlywindow.cpp來完成主函式
使用connect()函式將2個按鈕與進度條功能連結在一起
startProgress()為在按下按鈕<開始>後,進度條要執行的函式
resetProgress()為在按下按鈕<清空>後,進度條要執行的函式
#include "onlywindow.h"
#include "ui_onlywindow.h"
onlywindow::onlywindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::onlywindow)
{
ui->setupUi(this);
connect(ui->start, SIGNAL(clicked()), this, SLOT(startProgress()));
connect(ui->reset, SIGNAL(clicked()), this, SLOT(resetProgress()));
}
void onlywindow::startProgress(){
for(int i=0;i<=1000;i++){
ui->PBar->setValue(i);
QString str=QString("%1").arg(i);
str="複製數量: "+ str;
ui->label01->setText(str);
}
}
void onlywindow::resetProgress(){
ui->PBar->setValue(0);
ui->label01->setText("複製數量: 0");
}
onlywindow::~onlywindow()
{
delete ui;
}