Qt 5.10 Book Examples
chapter38
ThreadEvent
main.cpp
Go to the documentation of this file.
1
// ======================================================================
2
// main.cpp
3
// ======================================================================
4
// This file is a part of the book
5
// "Qt 5.10 Professional programming with C++"
6
// http://qt-book.com
7
// ======================================================================
8
// Copyright (c) 2017 by Max Schlee
9
// ======================================================================
10
11
#include <QtWidgets>
12
13
// ======================================================================
14
class
ProgressEvent
:
public
QEvent {
15
private
:
16
int
m_nValue;
17
18
public
:
19
enum
{
ProgressType
= User + 1};
20
21
ProgressEvent
() : QEvent((Type)
ProgressType
)
22
{
23
}
24
25
void
setValue
(
int
n)
26
{
27
m_nValue = n;
28
}
29
30
int
value
()
const
31
{
32
return
m_nValue;
33
}
34
};
35
36
// ======================================================================
37
class
MyThread
:
public
QThread {
38
private
:
39
QObject* m_pobjReceiver;
40
41
public
:
42
MyThread
(QObject* pobjReceiver) : m_pobjReceiver(pobjReceiver)
43
{
44
}
45
46
void
run
()
47
{
48
for
(
int
i = 0; i <= 100; ++i) {
49
usleep(100000);
50
51
ProgressEvent
* pe =
new
ProgressEvent
;
52
pe->
setValue
(i);
53
QApplication::postEvent(m_pobjReceiver, pe);
54
}
55
}
56
};
57
58
// ======================================================================
59
class
MyProgressBar
:
public
QProgressBar {
60
public
:
61
MyProgressBar
(QWidget* pwgt = 0) : QProgressBar(pwgt)
62
{
63
}
64
65
void
customEvent
(QEvent* pe)
66
{
67
if
((
int
)pe->type() ==
ProgressEvent::ProgressType
) {
68
setValue(((
ProgressEvent
*)(pe))->value());
69
}
70
QWidget::customEvent(pe);
71
}
72
};
73
74
// ----------------------------------------------------------------------
75
int
main
(
int
argc,
char
** argv)
76
{
77
QApplication app(argc, argv);
78
MyProgressBar
prb;
79
MyThread
thread(&prb);
80
81
prb.show();
82
83
thread.start();
84
85
return
app.exec();
86
}
ProgressEvent::value
int value() const
Definition:
main.cpp:30
ProgressEvent
Definition:
main.cpp:14
main
int main(int argc, char **argv)
Definition:
main.cpp:15
MyThread
Definition:
main.cpp:37
ProgressEvent::ProgressType
Definition:
main.cpp:19
ProgressEvent::ProgressEvent
ProgressEvent()
Definition:
main.cpp:21
MyProgressBar::MyProgressBar
MyProgressBar(QWidget *pwgt=0)
Definition:
main.cpp:61
MyThread::run
void run()
Definition:
main.cpp:46
ProgressEvent::setValue
void setValue(int n)
Definition:
main.cpp:25
MyProgressBar::customEvent
void customEvent(QEvent *pe)
Definition:
main.cpp:65
MyThread::MyThread
MyThread(QObject *pobjReceiver)
Definition:
main.cpp:42
MyProgressBar
Definition:
main.cpp:59
Generated by
1.8.14