Tian
Tian

Reputation: 51

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

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?

Upvotes: 5

Views: 1663

Answers (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.

Upvotes: 2

MahlerFive
MahlerFive

Reputation: 5279

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.

Upvotes: 8

Related Questions