当前位置:网站首页>5 connection modes of QT signal slot

5 connection modes of QT signal slot

2022-06-25 15:06:00 Knowledge first

qt The connection mode defined by the source code is as follows :
 Insert picture description here
1、Qt::AutoConnection
Generally, the fifth parameter cannot be written , In fact, the default value used , Using this value, the connection type will be determined during signal transmission .
 Insert picture description here

  • If the receiver and sender are in the same thread , Then automatically use Qt::DirectConnection type .
  • If the receiver and sender are not in the same thread , Then automatically use Qt::QueuedConnection type .

2、Qt::DirectConnection
The slot function will be called directly when the signal is sent , The slot function runs on the thread where the signal sender is located , It's kind of like a callback function .
The effect looks like calling the slot function directly at the signal sending position . This is dangerous in a multithreaded environment , May cause a run down .

3、Qt::QueuedConnection:
The slot function is called when it controls the event loop back to the recipient's thread , The slot function runs on the thread where the signal receiver is located .
After sending the signal , The slot function will not be called immediately , Wait until the receiver's current function is executed , After entering the event loop , The slot function will be called . This is generally used in multithreaded environments .

4、Qt::BlockingQueuedConnection
The call timing of the slot function is the same as Qt::QueuedConnection Agreement , However, after sending the signal, the sender's thread will block , Until the slot function runs .
Use this connection method , The receiver and sender must not be in the same thread , Otherwise the program will deadlock !!! This may be required when synchronization is required between multiple threads .

5、Qt::UniqueConnection
This flag It can be done by biting or (|) Used in combination with the above four . When this flag When setting , When a signal is connected to a slot , Repeated connections will fail . That is to avoid repeated connections .
I always thought UniqueConnection Should not be of connection type , It belongs to the connection property .

原网站

版权声明
本文为[Knowledge first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200512307922.html