-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.h
54 lines (46 loc) · 1.33 KB
/
Controller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
// SYSTEM INCLUDES
#include <QObject>
#include <QThread>
// APPLICATION INCLUDES
#include "Worker.h"
// DEFINES
// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STRUCTS
// TYPEDEFS
// FORWARD DECLARATIONS
class Controller : public QObject {
Q_OBJECT
QThread workerThread;
public:
Controller() {
auto worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::requestWork, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
connect(worker, &Worker::progressUpdate, this, [this](int secsLeft) {
emit onWorkProgress(secsLeft);
});
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
// Stack Overflow End
workerThread.start();
}
~Controller() {
if (workerThread.isRunning()) {
workerThread.quit();
workerThread.wait();
}
}
public slots:
void handleResults(const QString &result) {
// update the mainWindow with the final result
emit onWorkFinished(result);
}
signals:
void onWorkFinished(const QString&);
void onWorkProgress(int secsLeft);
void requestWork(int);
};