Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit b68ab0f

Browse files
author
mttwc
committed
Demonstrates progress bars
1 parent 55edeca commit b68ab0f

File tree

6 files changed

+100
-126
lines changed

6 files changed

+100
-126
lines changed

HelloQt.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
QT += widgets
77

88
SOURCES = src/main.cpp \
9-
src/list_demonstration.cpp \
9+
src/progress_bar_demonstration.cpp \
1010

11-
HEADERS = src/list_demonstration.h
11+
HEADERS = src/progress_bar_demonstration.h
1212

1313
CONFIG -= app_bundle
1414
CONFIG += c++11

src/list_demonstration.cpp

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/list_demonstration.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <QApplication>
22
#include <QWidget>
3-
#include "list_demonstration.h"
3+
#include "progress_bar_demonstration.h"
44

55
int main(int argc, char *argv[])
66
{
77
QApplication app(argc, argv);
88

9-
ListDemonstration window;
10-
11-
window.setWindowTitle("List Demonstration");
9+
ProgressBarDemonstration window;
10+
11+
window.resize(250, 150);
12+
window.setWindowTitle("Progress Bar Demonstration");
1213
window.show();
1314

1415
return app.exec();

src/progress_bar_demonstration.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "progress_bar_demonstration.h"
2+
#include <QTimer>
3+
#include <QGridLayout>
4+
5+
ProgressBarDemonstration::ProgressBarDemonstration(QWidget *parent)
6+
: QWidget(parent)
7+
{
8+
progress = 0;
9+
timer = new QTimer(this);
10+
connect(timer, &QTimer::timeout, this, &ProgressBarDemonstration::updateBar);
11+
12+
QGridLayout *grid = new QGridLayout(this);
13+
grid->setColumnStretch(2, 1);
14+
15+
pbar = new QProgressBar();
16+
grid->addWidget(pbar, 0, 0, 1, 3);
17+
18+
startBtn = new QPushButton("Start", this);
19+
connect(startBtn, &QPushButton::clicked, this, &ProgressBarDemonstration::startTimer);
20+
grid->addWidget(startBtn, 1, 0, 1, 1);
21+
22+
stopBtn = new QPushButton("Stop", this);
23+
connect(stopBtn, &QPushButton::clicked, this, &ProgressBarDemonstration::stopTimer);
24+
grid->addWidget(stopBtn, 1, 1, 1, 1);
25+
}
26+
27+
void ProgressBarDemonstration::startTimer()
28+
{
29+
if (progress > MAX_VALUE)
30+
{
31+
progress = 0;
32+
pbar->setValue(0);
33+
}
34+
35+
if (!timer->isActive())
36+
{
37+
startBtn->setEnabled(false);
38+
stopBtn->setEnabled(true);
39+
timer->start(DELAY); // The timeout signal is emitted every DELAY milliseconds
40+
}
41+
}
42+
43+
void ProgressBarDemonstration::stopTimer()
44+
{
45+
if (timer->isActive())
46+
{
47+
startBtn->setEnabled(true);
48+
stopBtn->setEnabled(false);
49+
timer->stop();
50+
}
51+
}
52+
53+
void ProgressBarDemonstration::updateBar()
54+
{
55+
progress++;
56+
57+
if (progress <= MAX_VALUE)
58+
{
59+
pbar->setValue(progress);
60+
}
61+
else
62+
{
63+
timer->stop();
64+
startBtn->setEnabled(true);
65+
stopBtn->setEnabled(false);
66+
}
67+
}

src/progress_bar_demonstration.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <QWidget>
4+
#include <QProgressBar>
5+
#include <QPushButton>
6+
7+
class ProgressBarDemonstration : public QWidget
8+
{
9+
Q_OBJECT
10+
11+
public:
12+
ProgressBarDemonstration(QWidget *parent = 0);
13+
14+
private:
15+
int progress;
16+
QTimer *timer;
17+
QProgressBar *pbar;
18+
QPushButton *startBtn;
19+
QPushButton *stopBtn;
20+
static const int DELAY = 200;
21+
static const int MAX_VALUE = 100;
22+
23+
void updateBar();
24+
void startTimer();
25+
void stopTimer();
26+
};

0 commit comments

Comments
 (0)