The semaphore mechanism of the thread needs to turn on the event loop mechanism of the thread , That is to call QThread::exec() Function to start the thread's event loop .
Qt The signal - The prototype of the slot connection function is as follows ：
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection )
Qt Support 5 Kind of connection
- Qt::DirectConnection（ Direct connection ）（ The relationship between signal and slot function is similar to function call , Synchronous execution ）
When the signal is sent , The corresponding slot function will be called immediately .emit The code after the statement will be executed after all slot functions have been executed . When the signal is transmitted , The slot function will be called directly . No matter which thread the slot function belongs to , Slot functions are executed within the thread that emits the signal .
- Qt::QueuedConnection（ Queue mode ）（ At this point, the signal is jammed into the event queue , The relationship between signal and slot function is similar to message communication , Asynchronous execution ）
When the signal is sent , Queue up in the signal queue , The signal is not obtained until the event loop of the thread to which the receiving object belongs takes control , Call the corresponding slot function .emit The code after the statement will be executed immediately after the signal is issued , There is no need to wait 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 ）
Qt The default connection mode , If the object sending and receiving signals belong to the same thread , That works the same way as a direct connection ; Otherwise, it works the same way as the queue . If the signal is emitted within the thread to which the receiver is attached , It is equivalent to a direct connection if the sending thread is different from the thread to which the receiver is attached , Is equivalent to a queue connection
- Qt::BlockingQueuedConnection（ Signals and slots must be in different threads , Otherwise, a deadlock will occur )
The call case of slot function and Queued Connection identical , The difference is that the current thread will block , Until the slot function returns .
Works the same way as the default , It's just that you can't repeatedly connect the same signal and slot , Because if the connection is repeated, a signal will be sent out , The corresponding slot function will be executed several times .
If there is no special request, we connect Function to select the default connection method , That is to say connect If you don't fill in the fifth parameter, just ok, for example ：
connect(m_obj, &QObject::destroyed, this, &MainWindow::SetPtrNullptr);