4/9/2022»»Saturday

Slot In Qthread

4/9/2022
    90 - Comments
  1. Signal And Slot In Qthread
  2. Invoke Slot In Qthread

There are two way to use QThread:

  • Subclass QThread and reimplement its run() function
  • Use worker objects by moving them to the thread

This brings us to a fundamental aspect of QThread: it works seamlessly with the signal/slot mechanism. Qt is an event-driven framework, where a main event loop (or the GUI loop) processes events (user input, graphical, and so on) to refresh the UI. Each QThread comes with its. The custom output (QRect, QImage) signal is connected to the addImage slot so that we can update the viewer label every time a new star is drawn.

As the QThread::run() is the entry point of worker thread, so the former usage is rather easy to understand.

In this article, we will try to figure out in which way the latter usage works.

Event Loop

As a event direvn programming framework, Qt make use of event loop widely. For example, following functions are used in nearly every Qt program.

Each of them will create a QEventLoop object, and run it. Take QCoreApplication as an example,

Conceptually, the event loop looks like this:

Each thread has its own event queue, note that, event queue is belong to thread instead of event loop, and it's shared by all the event loops running in this thread.

When the event loop find that its event queue is not empty, it will process the events one by one. Eventually, the QObject::event() member of the target object get called.

Seems it's really not easy to understand how the event system works without a example. So we create a demo

Example

In this example,

First, we

Signal And Slot In Qthread

  • Create a custom Event new QEvent(QEvent::User)
  • Post the Event to a queue QCoreApplication::postEvent()

Then,

  • The Event is discovered by the event loop in the queue QApplication::exec()
  • The Test::event() get called by the event loop.

In this example, the Test::event() get called in the main thread. What should we do if want to run it in a work thread??

Thread Affinity

Qthread

As each thread have its own event queue, so there will be more than one event queues exists in one multi-thread program. So which event queue will be used when we post a event?

Let's have a look at the code of postEvent().

As you can see, the event queue is found through the receiver's thread property. This thread is called the thread affinity - what thread the QObject 'lives' in. Normally, it's the thread in which the object was created, but it can be changed using QObject::moveToThread().

Please note that, QCoreApplication::postEvent() is thread safe, as QMutex has been used here.

Now, it's easy to run the event process it worker thread instead of main thread.

Example

Add three lines to the main() function of last example.

Invoke Slot In Qthread

The output of application will be

while the output of last example was

Queued Connection

For queued connection, when the signal is emitted, a event will be post to the event queue.

Then, this event will be found by the event queued, and finally, QObject::event() will be called in the thread.

As QCoreApplication::postEvent() is thread safe, so if you interact with an object only using queued signal/slot connections, then the usual multithreading precautions need not to be taken any more.

Reference