반응형
timer를 사용 ProgressBar 증가 시키기
step1. UI 디자인
step2. 타이머 라이브러리 추가
mainwindow.cpp 파일에 타이머 라이브러리를 추가 해줍니다.
#include <QTimer>
step3. 헤더파일에 변수와 함수 추가
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QTimer *timer; // 타이머 인스턴스를 저장할 변수
int tick; // 타이머 틱 변수
private slots:
int timer_update(); // 20ms마다 수행 되어지는 call back funtion
void on_pushButton_start_clicked();
void on_pushButton_end_clicked();
};
#endif // MAINWINDOW_H
step4. mainwindow.cpp 작성
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer> // 타이머 라이브러리
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this); // 타이머라는 인스턴스 생성 후 저장
connect(timer, SIGNAL(timeout()),this,SLOT(timer_update())); // 타이머 맵핑
tick = 0; // 타이머 연결
// timer -> start(20); // 20ms마다 timer 가동
// timer -> stop(); // 타이머 중지
}
int MainWindow::timer_update()
{
if (tick >= 100)
{
ui -> label -> setText("Loading Complete !!!");
}
else if (tick < 100)
{
tick++;
ui -> label -> setText("Loading....");
}
ui -> progressBar -> setValue(tick);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_start_clicked()
{
timer -> start(20);
}
void MainWindow::on_pushButton_end_clicked()
{
timer -> stop();
}
코드
GitHub - kyw6416/Qt_ProgressBar
Contribute to kyw6416/Qt_ProgressBar development by creating an account on GitHub.
github.com
동작영상
반응형