Created
July 26, 2013 06:59
-
-
Save paulbarbu/6086849 to your computer and use it in GitHub Desktop.
QRunnable POC
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 "foo.h" | |
#include <QThread> | |
#include <QDebug> | |
Foo::Foo(int d, QObject *parent) : | |
QObject(parent), data(d) | |
{ | |
} | |
void Foo::run() | |
{ | |
qDebug() << "run() in: " << QThread::currentThread(); | |
connect(this, SIGNAL(startWorking()), this, SLOT(doWork())); | |
emit startWorking(); | |
eventLoop.exec(); | |
} | |
void Foo::doWork() | |
{ | |
qDebug() << "doWork() in: " << QThread::currentThread(); | |
} |
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
#ifndef FOO_H | |
#define FOO_H | |
#include <QObject> | |
#include <QRunnable> | |
#include <QEventLoop> | |
class Foo : public QObject, public QRunnable | |
{ | |
Q_OBJECT | |
public: | |
explicit Foo(int data, QObject *parent = 0); | |
void run(); | |
signals: | |
void startWorking(); | |
public slots: | |
void doWork(); | |
private: | |
QEventLoop eventLoop; | |
int data; | |
}; | |
#endif // FOO_H |
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 <QCoreApplication> | |
#include <QThreadPool> | |
#include "foo.h" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
Foo *foo = new Foo(42); | |
QThreadPool::globalInstance()->start(foo); | |
return a.exec(); | |
} |
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
QT += core | |
QT -= gui | |
TARGET = qrunnable_poc | |
CONFIG += console | |
CONFIG -= app_bundle | |
TEMPLATE = app | |
SOURCES += main.cpp \ | |
foo.cpp | |
HEADERS += \ | |
foo.h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment