In Qt many slots connected to the same signal, do they invoke in order when emit the signal?

谁说胖子不能爱 提交于 2019-12-23 12:46:30

问题


In the Qt document it is said:

if several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

But in the connect() function, setting the Qt::ConnectionType type as Qt::QueuedConnection means "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." and Qt::DirectConnection means "the slot is invoked immediately, when the signal is emitted." The slots maybe not execute in order.

Are they conflicting?


回答1:


If multiple slots have a Qt::DirectConnection, they will be invoked in the order they were connected. If multiple slots have a Qt::QueueConnection, they will be invoked in the order they were connected. If you mix and match, then all Qt::DirectionConnection slots will invoke in order, then when control returns to event loop, all the Qt::QueuedConnection slots will invoke in order.




回答2:


Note, though, that even though the slot invocation order is known, depending on it will almost always lead to brittle code. Connections are meant to be dynamic. It's rather hard for them to be dynamic when you depend on the order of connections remaining static. If you have code that really depends on the connection order, you should refactor it so that the order of execution is controlled some other way, or otherwise it is made known to the maintainer of the code that the actions need to be sequenced.



来源:https://stackoverflow.com/questions/31751605/in-qt-many-slots-connected-to-the-same-signal-do-they-invoke-in-order-when-emit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!