Emitting signal from the same slot it is connected to.
-
Can i emit the signal which slot is connected from the same slot again. I will make the emit from slot conditional and happen only once,to avoid recursive calling.Can this lead to an issue?
I'm working with a Qt application involving signals and slots. A signal is emitted from a state machine running in a separate thread, initiated by a function in Class A. After the state machine completes, it emits a signal connected to a slot in Class B. In the slot execution, I'm calling the same function to run the state machine again. Could this approach potentially cause issues?
-
@candidaj said in Emitting signal from the same slot it is connected to.:
Can this lead to an issue?
If you make sure you do not have an endless recursion it should be OK
-
-
Recursion can always lead to a place where the stack is too deeply nested. Modern compilers can convert tail recursion into a loop to avoid this problem. However, signals and slots will prevent this.
This would only be a problem if you are using a DirectConnection for your signals and slots (which would most likely happen automatically). You should make sure that your connections (for the state machine) are QueuedConnections. Especially if the state machine is run inside the main thread a DirectConnection would block the event loop. A QueuedConnection would allow other things to be processed by the event loop as well.