P
P
pixik2015-07-15 14:30:56
Qt
pixik, 2015-07-15 14:30:56

How does the QT signal-slot mechanism work if you need to send a signal from an object of one thread to a slot on an object of another thread?

Hello!
By itself, the principle of signal-slot operation in qt is clear to me (when one object sends a signal to the slot of another object in one thread). I got completely confused when I tried to figure out how to transfer a signal from an object located in one thread to a slot to an object located in another thread.
Who understands, please explain. I didn’t manage to google intelligible information, except for the one that completely confused me.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2015-07-15
@Nipheris

The key to understanding signaling is understanding the raison d'être of QThread:
> 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 Qt event loop inside the thread.
Launching your own message processing loop and making it possible to receive them, including from other threads. The feature of the signals / slots of the kit is that it is also a simple message queue system, similar to the Erlang one, if encountered. A call within a single thread is a direct call to signed slots on the same stack, but a QueuedConnection or an inter-thread call puts a message in the queue of the corresponding thread (these messages are in the same queue with messages from the OS), and the thread will retrieve them in the event-loop -e. Thus, you simply ask another thread to call the slot, and it will call it when it once again completes the message loop and it reaches the slot call message.
> It is safe to connect signals and slots across different threads, thanks to a mechanism called queued connections.
doc.qt.io/qt-5.4/qt.html#ConnectionType-enum
Qt::QueuedConnection - The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question