- A safe way of calling a slot in another thread is by placing that call in another thread's event loop.
- Cross Thread Signals and Slots. At emit time Qt compares thread ids. The id of the current thread calling emit signal. The id the receiver belongs to via obj-thread. If the threads are the same slots are called. If different an event is packaged/posted 47.
- Connect the signals to the GUI thread's slots using queued signal/slot connections. Permanent: Have an object living in another thread and let it perform different tasks upon request. This means communication to and from the worker thread is required.
Qt Cross Thread Signal Slotted
The signal slot mechanism of the thread needs to turn on the event loop mechanism of the thread, that is to call the qthread:: exec() function to start the event loop of the thread.
The QThread::run method only spins (exec in Qt parlance) an event loop (of course it won't do it anymore if your reimplementation doesn't). An event loop waits for events to arrive in the queue, and then notifies the target QObjects of their reception. The cross-thread (queued) signal-slot connections are implemented by leveraging events.
The prototype of QT signal slot connection function is as follows:
QT supports five connection modes
- QT:: directconnection(the relationship between signal and slot function is similar to function call and executed synchronously)
When the signal is sent, the corresponding slot function will be called immediately. The code after the emit statement will be executed after all slot functions have been executed. When the signal is transmitted, the slot function is called directly. No matter which thread the slot function belongs to, the slot function is executed in the thread that emits the signal.
- QT:: queuedconnection(at this time, the signal is jammed into the event queue, and the relationship between the signal and the slot function is similar to message communication and executed asynchronously)
When the signal is sent out, it is queued to the signal queue, and the signal is obtained only when the event loop of the thread to which the receiving object belongs obtains control, and the corresponding slot function is called. The code after the emit statement is executed immediately after the signal is issued, without waiting for the slot function to complete. When control returns to the event loop of the thread to which the receiver is attached, the slot function is called. The slot function is executed in the thread to which the receiver is attached.
- QT:: autoconnection (automatic mode)
The default connection mode of QT. If the signal sending and receiving objects belong to the same thread, the working mode is the same as the direct connection mode; otherwise, the working mode is the same as the queue mode. If the signal is emitted in the thread to which the receiver is attached, it is equivalent to a direct connection. If the thread transmitting the signal is different from the thread attached to the receiver, it is equivalent to a queue connection
- Qt::BlockingQueuedConnection(the signal and slot must be in different threads, otherwise a deadlock will occur)
The call situation of slot function is the same as that of queued connection, except that the current thread will block until the slot function returns.
It works in the same way as the default, except that the same signal and slot cannot be connected repeatedly, because if the repeated connection will cause a signal to be issued, the corresponding slot function will be executed multiple times.
Qt Signals And Slots Tutorial
If there are no special requirements, we can select the default connection method for the connect function, that is, the fifth parameter of connect is OK without filling in, for example: