diff options
author | Sze Howe Koh <szehowe.koh@gmail.com> | 2012-10-08 13:41:33 (GMT) |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-10-20 11:35:10 (GMT) |
commit | 207f588b6896cbe72745037dc1cb0a3aef1cf6d0 (patch) | |
tree | 956ad0534047a102d168f962b0644f716a6a2001 /doc | |
parent | ffd4ecb5a21ef97d0874c7c220bc0dbbd82daa52 (diff) | |
download | Qt-207f588b6896cbe72745037dc1cb0a3aef1cf6d0.zip Qt-207f588b6896cbe72745037dc1cb0a3aef1cf6d0.tar.gz Qt-207f588b6896cbe72745037dc1cb0a3aef1cf6d0.tar.bz2 |
Doc: Update QThread class ref to reflect changes since Qt 4.4
Remove advice to subclass QThread; promote thinking of QThread as a
thread manager, not a thread; promote event-driven programming over time
micromanagement; warn against common pitfalls.
Result of collaboration in forum (https://qt-project.org/forums/viewthread/20691/)
and mailing list (http://lists.qt-project.org/pipermail/development/2012-September/006738.html)
This is a modified backport of
qtbase commit d4ad9dbbf96884c0899e8f8116a8a056facd52d5
Task-number: QTBUG-16358
Change-Id: Ib7a8d40aa418b1d96e3e7070df074b396ae9ee7e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/src/snippets/code/src_corelib_thread_qthread.cpp | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/doc/src/snippets/code/src_corelib_thread_qthread.cpp b/doc/src/snippets/code/src_corelib_thread_qthread.cpp index 420f035..1a07744 100644 --- a/doc/src/snippets/code/src_corelib_thread_qthread.cpp +++ b/doc/src/snippets/code/src_corelib_thread_qthread.cpp @@ -39,18 +39,40 @@ ****************************************************************************/ //! [0] -class MyThread : public QThread +class Worker : public QObject { -public: - void run(); + Q_OBJECT + +public slots: + void doWork() { + ... + } }; -void MyThread::run() +void MyObject::putWorkerInAThread() { - QTcpSocket socket; - // connect QTcpSocket's signals somewhere meaningful - ... - socket.connectToHost(hostName, portNumber); - exec(); + Worker *worker = new Worker; + QThread *workerThread = new QThread(this); + + connect(workerThread, SIGNAL(started()), worker, SLOT(doWork())); + connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater())); + worker->moveToThread(workerThread); + + // Starts an event loop, and emits workerThread->started() + workerThread->start(); } //! [0] + +//! [1] +class AdvancedThreadManager : public QThread +{ +protected: + void run() + { + /* ... other code to initialize thread... */ + + // Begin event handling + exec(); + } +}; +//! [1] |