When to use Qt::DirectConnection and QueuedConnection
Solved
QML and Qt Quick
-
Hi,
When should we use Direct Connection and Queued Connection while writing a connect Statement.
The Signal in the connect statement is in a different thread and the Slot emits a signal that needs to update a value in the UI(QML).
So how do we decide as to which is the best connection type to use.
Thank You
-
If it is in a different thread, you should use QueuedConnection for sure. This way you can be certain no thread synchronization issues occur.
Or, if you leave connection type argument empty, Qt will choose the right type automatically for you (queued for threaded connections, direct otherwise).
You can also use QueuedConnection in single-thread connections to delay slot execution a bit, allow the event loop to spin etc.
In short:
- direct connection is the same as direct function call - it is called immediately and in the same thread
- queued connection is inserted into event loop and will be called "some time in the future, by receiving thread". Where "some time" is usually "very quickly"