|
| QThread (QObject *parent=nullptr) |
|
| ~QThread () |
|
bool | event (QEvent *event) override |
|
QAbstractEventDispatcher * | eventDispatcher () const |
|
void | exit (int returnCode=0) |
|
bool | isFinished () const |
|
bool | isInterruptionRequested () const |
|
bool | isRunning () const |
|
int | loopLevel () const |
|
Priority | priority () const |
|
void | requestInterruption () |
|
void | setEventDispatcher (QAbstractEventDispatcher *eventDispatcher) |
|
void | setPriority (Priority priority) |
|
void | setStackSize (uint stackSize) |
|
uint | stackSize () const |
|
bool | wait (unsigned long time=ULONG_MAX) |
|
| QObject (QObject *parent=nullptr) |
|
| ~QObject () |
|
bool | blockSignals (bool block) |
|
const QList< QObject * > & | children () const |
|
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
|
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
|
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
|
void | dumpObjectInfo () |
|
void | dumpObjectTree () |
|
QList< QString > | dynamicPropertyNames () const |
|
virtual bool | eventFilter (QObject *watched, QEvent *event) |
|
template<typename T > |
T | findChild (const QString &childName=QString ()) const |
|
template<class T > |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
|
template<class T > |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
|
bool | inherits (const QString &className) const |
|
void | installEventFilter (QObject *filterObj) |
|
bool | isWidgetType () const |
|
bool | isWindowType () const |
|
void | killTimer (int id) |
|
const QMetaObject * | metaObject () const |
|
void | moveToThread (QThread *targetThread) |
|
QString | objectName () const |
|
QObject * | parent () const |
|
template<class T = QVariant> |
T | property (const QString &name) const |
|
void | removeEventFilter (QObject *obj) |
|
void | setObjectName (const QString &name) |
|
void | setParent (QObject *parent) |
|
bool | setProperty (const QString &name, const QVariant &value) |
|
bool | signalsBlocked () const |
|
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
|
QThread * | thread () const |
|
|
static QThread * | currentThread () |
|
static Qt::HANDLE | currentThreadId () |
|
static int | idealThreadCount () |
|
static void | msleep (unsigned long msecs) |
|
static void | sleep (unsigned long secs) |
|
static void | usleep (unsigned long usecs) |
|
static void | yieldCurrentThread () |
|
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
|
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
|
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
|
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
|
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
|
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
|
static QMetaObject & | staticMetaObject () |
|
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
|
The QThread class provides a platform independent way to manage threads. A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a CopperSpice event loop inside the thread.
You can use worker objects by moving them to the thread using QObject::moveToThread().
{
CS_OBJECT(Worker)
public:
CS_SIGNAL_1(Public,
void resultReady(
const QString &result))
CS_SIGNAL_2(resultReady, result)
CS_SLOT_1(Public,
void doWork(
const QString ¶meter) { emit resultReady(result);})
CS_SLOT_2(doWork)
};
{
CS_OBJECT(Controller)
public:
Controller() {
Worker *worker = new Worker;
}
~Controller() {
}
public:
CS_SIGNAL_1(Public,
void operate(
const QString &data))
CS_SIGNAL_2(operate, data)
CS_SLOT_1(Public,
void handleResults(
const QString &))
CS_SLOT_2(handleResults)
};
The code inside the Worker's slot would then execute in a separate thread. However, you can connect the Worker's slots to any signal, from any object, in any thread. It is safe to connect signals and slots across different threads due to the queued connection mechanism. Refer to Qt::ConnectionType for additional information.
Another way to make code run in a separate thread, is to subclass QThread and reimplement run().
class WorkerThread :
public QThread
{
CS_OBJECT(WorkerThread)
emit resultReady(result);
}
public:
CS_SIGNAL_1(Public,
void resultReady(
const QString &s))
CS_SIGNAL_2(operate, s)
};
void MyObject::startWorkInAThread()
{
WorkerThread *workerThread = new WorkerThread(this);
}
In that example the thread will exit after the run function has returned. There will not be any event loop running in the thread unless you call exec().
It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread.
When subclassing QThread, keep in mind that the constructor executes in the old thread while run() executes in the new thread. If a member variable is accessed from both functions, then the variable is accessed from two different threads. Check that it is safe to do so.
Care must be taken when interacting with objects across different threads. Refer to Synchronizing Threads for details.
Managing Threads
QThread will notify you via a signal when the thread is started(), finished(), and terminated(), or you can use isFinished() and isRunning() to query the state of the thread.
You can stop the thread by calling exit() or quit(). In extreme cases you may want to forcibly terminate() an executing thread. However doing so is dangerous and discouraged. Please read the documentation for terminate() and setTerminationEnabled() for detailed information.
It is possible to deallocate objects that live in a thread that has just ended by connecting the finished() signal to QObject::deleteLater().
The static functions currentThreadId() and currentThread() return identifiers for the currently executing thread. The former returns a platform specific ID for the thread, the latter returns a QThread pointer.
To choose the name that your thread will be given (as identified by the command ps -L
on Linux, for example), you can call setObjectName() before starting the thread. If you do not call setObjectName(), the name given to your thread will be the class name of the runtime type of your thread object. This feature is not available with release builds on Windows.
Blocking Threads
Use wait() to block the calling thread, until the other thread has finished execution (or until a specified time has passed). QThread also provides static, platform independent sleep functions: sleep(), msleep(), and usleep() allow full second, millisecond, and microsecond resolution respectively.
- Note
- The wait() and sleep() methods should be avoided since CopperSpice is an event driven system. Instead of wait(), consider listening for the finished() signal. Instead of the sleep() methods consider using QTimer.
- See also
- Multithreading, QThreadStorage, Synchronizing Threads