by admin

Qt Signal Slot Thread Context

Example

Qt Signal Slot Parameter

The thread that the signal receiver lives in will then run the slot. Alternatively, call QMetaObject::invokeMethod to achieve the same effect without signals. In both cases, a queued connection must be used because a direct connection bypasses the event system and runs the method immediately in the current thread. Signals & Slots Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system. Let`s imagine that we have 2 different Qt objects living in different threads. The first object emits signal to connected slot in the object 2. The second object calls request slot in the second thread context, than emits signal to connected slot in the object 1. The first object calls response slot in the first thread context, correspondingly. Sig4j is a Java library for Qt like signals and slots which uses the FunctionalInterface Annotation introduced with Java 8. This Annotation allows sig4j to connect functions and lambdas to signals without much effort. The following code snippet gives a short example.

Some times you see a signal is emitted in sender thread but connected slot doesn't called (in other words it doesn't receive signal), you have asked about it and finaly got that the connection type Qt::DirectConnection would fix it, so the problem found and everything is ok.

But generaly this is bad idea to use Qt:DirectConnection until you really know what is this and there is no other way. Lets explain it more, Each thread created by Qt (including main thread and new threads created by QThread) have Event loop, the event loop is responsible for receiving signals and call aproporiate slots in its thread. Generaly executing a blocking operation inside an slot is bad practice, because it blocks the event loop of that threads so no other slots would be called.

If you block an event loop (by making very time consuming or blocking operation) you will not receive events on that thread until the event loop will be unblocked. If the blocking operation, blocks the event loop forever (such as busy while), the slots could never be called.

In this situation you may set the connection type in connect to Qt::DirectConnection, now the slots will be called even the event loop is blocked. so how this could make broke everything? In Qt::DirectConnection Slots will be called in emiter threads, and not receiver threads and it can broke data synchronizations and ran into other problems. So never use Qt::DirectConnection unless you know what are you doing. If your problem will be solved by using Qt::DirectConnection, you have to carefull and look at your code and finding out why your event loop is blocked. Its not a good idea to block the event loop and its not recomended in Qt.

Here is small example which shows the problem, as you can see the nonBlockingSlot would be called even the blockingSlot blocked event loop with while(1) which indicates bad coding

Qt signal slot performanceSignalQt Signal Slot Thread Context
Context

Qt Signal Slot Thread


Qt Signal Thread

Related Tags