summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc
diff options
context:
space:
mode:
authorSze Howe Koh <szehowe.koh@gmail.com>2012-10-04 19:33:39 +0800
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-10-08 09:16:12 +0200
commitd4ad9dbbf96884c0899e8f8116a8a056facd52d5 (patch)
tree763e7603c874ec8951846c1348105cfe03b221d9 /src/corelib/doc
parentbac9d5921f0da609eedd72e371dae7c8321dc410 (diff)
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 (https://qt-project.org/pipermail/development/2012-September/006738.html) Task-number: QTBUG-16358 Change-Id: I57e3873976fad489176cbf9f7e680fd6992a8837 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp40
1 files changed, 31 insertions, 9 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 420f035d0d..0429dac6eb 100644
--- a/src/corelib/doc/snippets/code/src_corelib_thread_qthread.cpp
+++ b/src/corelib/doc/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, &QThread::started, worker, &Worker::doWork);
+ connect(workerThread, &QThread::finished, worker, &Worker::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]