diff options
Diffstat (limited to 'doc/src')
-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] |