Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_corelib_thread_qthread.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtCore/QThread>
5class MyObject;
6
8class WorkerThread : public QThread
9{
11 void run() override {
13 /* ... here is the expensive or blocking operation ... */
15 }
17 void resultReady(const QString &s);
18};
19
20void MyObject::startWorkInAThread()
21{
22 WorkerThread *workerThread = new WorkerThread(this);
23 connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
24 connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
25 workerThread->start();
26}
28
29
31class Worker : public QObject
32{
34
35public slots:
36 void doWork(const QString &parameter) {
38 /* ... here is the expensive or blocking operation ... */
40 }
41
44};
45
46class Controller : public QObject
47{
49 QThread workerThread;
50public:
52 Worker *worker = new Worker;
53 worker->moveToThread(&workerThread);
54 connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
57 workerThread.start();
58 }
60 workerThread.quit();
61 workerThread.wait();
62 }
63public slots:
64 void handleResults(const QString &);
66 void operate(const QString &);
67};
void handleResults(const QString &)
void operate(const QString &)
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
bool moveToThread(QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
Changes the thread affinity for this object and its children and returns true on success.
Definition qobject.cpp:1643
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void start(Priority=InheritPriority)
Definition qthread.cpp:996
void finished(QPrivateSignal)
void resultReady(const QString &s)
void resultReady(const QString &result)
void doWork(const QString &parameter)
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
#define Q_OBJECT
#define slots
#define signals
#define emit