Qt Quick Start: Signal and slot
Signals and slots are mainly used for communication between components, similar to delegation in. net and java.
Use the QObject: connect method to associate the signal with the slot. Then the signal initiator sends a signal, and the slot function in the receiver executes.
For example, connect (this, SIGNAL (start (), worker, SLOT (Start () associates the start SIGNAL in the current class with the Start function of the worker object, when we call emit this-> start (), the slot function will receive this signal.
The connect function has the fifth parameter, which determines when the signal will be sent to the slot. Here I will mention it, but I have always used the default value.
Qt: AutoConnection. The default value is automatic connection. When the signal initiator is in the same thread as the slot function executor, Qt: DirectConnection is used, and Qt: QueuedConnection is used.
Qt: DirectConnection, which is directly connected. The signal initiating slot function is executed immediately, and the signal initiator and performer are in the same thread.
Qt: QueuedConnection, queue type. The signal initiator and executor are not in the same thread. When the signal is initiated, the slot function will wait for scheduling.
Qt: BlockingQueuedConnection, which is the same as Qt: QueuedConnection. The difference is that there is a mutex lock when the signal is initiated. Wait for the return of the slot function, in this way, if the slot function and the signal function are in the same thread, a deadlock will occur (that is, they are stuck in the lock position and cannot enter the slot function ).
Qt: UniqueConnection, Which is used together with the above through "or", indicates that the same connection can only be connected once, and it seems useless, multiple connections with the same object, signal, and slot will fail.
To disconnect the signal from the slot, use the QObject: disconnect method.
There are so many signal and slot usage, but they are often used, especially in multithreading, when the slot function is not executed, it may be that the connection is wrong or the thread where the slot function is located is blocked (such as an endless loop ).