Created
October 28, 2019 16:51
-
-
Save ksvbka/3560da74c39d8ba4b65339b6463e5564 to your computer and use it in GitHub Desktop.
QThread demo using worker model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QObject> | |
#include <QTextEdit> | |
#include <QThread> | |
#include <QVBoxLayout> | |
#include <QWidget> | |
#include <QtWidgets> | |
class Worker : public QObject { | |
Q_OBJECT | |
public slots: | |
void doWork() { | |
QThread::sleep(5); | |
emit resultReady("Workers are the best"); | |
} | |
signals: | |
void resultReady(const QString &data); | |
}; | |
int main(int argc, char *argv[]) { | |
QApplication a(argc, argv); | |
/* Simple UI*/ | |
auto widget = new QWidget(); | |
auto vLayout = new QVBoxLayout(widget); | |
auto textEdit = new QTextEdit(widget); | |
vLayout->addWidget(textEdit); | |
vLayout->addWidget(new QPushButton("click me to test blocking UI", widget)); | |
/* Setup thread */ | |
auto thread = new QThread(widget); | |
auto worker = new Worker(); | |
worker->moveToThread(thread); | |
QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater); | |
QObject::connect(thread, &QThread::started, worker, &Worker::doWork); | |
QObject::connect(worker, &Worker::resultReady, textEdit, &QTextEdit::append); | |
/* Start working*/ | |
thread->start(); | |
widget->show(); | |
return a.exec(); | |
} | |
/* If define class QT in cpp file, have to add this include to bypass build | |
* error */ | |
/* https://stackoverflow.com/a/41844844 */ | |
#include "main.moc" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2019-06-17T22:08:10 | |
# | |
#------------------------------------------------- | |
QT += core gui | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = thread-worker | |
TEMPLATE = app | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which has been marked as deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | |
SOURCES += main.cpp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment