diff options
author | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2013-02-13 11:58:07 +0100 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2013-02-14 14:24:57 +0100 |
commit | e65cd6f3794e12e6bc5c2ee985eae8e70ff5f333 (patch) | |
tree | 8965835c375422d63b2ccfa927b31a56e64bda1d /src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp | |
parent | d1ee7189553e13337b198fe4ba66d79fb7a7f41d (diff) | |
parent | e95a758236cf2c68e33da4ddb62bff4fe8d9dd8b (diff) |
Merge remote-tracking branch 'origin/stable' into dev
Conflicts:
src/concurrent/doc/qtconcurrent.qdocconf
src/corelib/doc/qtcore.qdocconf
src/corelib/global/qglobal.h
src/dbus/doc/qtdbus.qdocconf
src/dbus/qdbusmessage.h
src/gui/doc/qtgui.qdocconf
src/gui/image/qimagereader.cpp
src/network/doc/qtnetwork.qdocconf
src/opengl/doc/qtopengl.qdocconf
src/opengl/qgl.h
src/plugins/platforms/windows/qwindowswindow.cpp
src/printsupport/doc/qtprintsupport.qdocconf
src/sql/doc/qtsql.qdocconf
src/testlib/doc/qttestlib.qdocconf
src/tools/qdoc/doc/config/qt-cpp-ignore.qdocconf
src/widgets/doc/qtwidgets.qdocconf
src/xml/doc/qtxml.qdocconf
Change-Id: Ie9a1fa2cc44bec22a0b942e817a1095ca3414629
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp | 79 |
1 files changed, 53 insertions, 26 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp index f0bc759320..c33c8bb48a 100644 --- a/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the documentation of the Qt Toolkit. @@ -38,41 +38,68 @@ ** ****************************************************************************/ -//! [0] -class Worker : public QObject +#include <QtCore/QThread> +class MyObject; + +//! [reimpl-run] +class WorkerThread : public QThread { Q_OBJECT - -public slots: - void doWork() { - ... + void run() Q_DECL_OVERRIDE { + QString result; + /* expensive or blocking operation */ + emit resultReady(result); } +signals: + void resultReady(const QString &s); }; -void MyObject::putWorkerInAThread() +void MyObject::startWorkInAThread() { - Worker *worker = new Worker; - QThread *workerThread = new QThread(this); - - connect(workerThread, &QThread::started, worker, &Worker::doWork); - connect(workerThread, &QThread::finished, worker, &Worker::deleteLater); - worker->moveToThread(workerThread); - - // Starts an event loop, and emits workerThread->started() + WorkerThread *workerThread = new WorkerThread(this); + connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults); + connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater); workerThread->start(); } -//! [0] +//! [reimpl-run] + -//! [1] -class AdvancedThreadManager : public QThread +//! [worker] +class Worker : public QObject { -protected: - void run() - { - /* ... other code to initialize thread... */ + Q_OBJECT + QThread workerThread; - // Begin event handling - exec(); +public slots: + void doWork(const QString ¶meter) { + // ... + emit resultReady(result); } + +signals: + void resultReady(const QString &result); +}; + +class Controller : public QObject +{ + Q_OBJECT + QThread workerThread; +public: + Controller() { + Worker *worker = new Worker; + worker->moveToThread(&workerThread); + connect(workerThread, &QThread::finished, worker, &QObject::deleteLater); + connect(this, &Controller::operate, worker, &Worker::doWork); + connect(worker, &Worker::resultReady, this, &Controller::handleResults); + workerThread.start(); + } + ~Controller() { + workerThread.quit(); + workerThread.wait(); + } +public slots: + void handleResults(const QString &); +signals: + void operate(const QString &); }; -//! [1] +//! [worker] |