C++ signal is emitted only once, but the QML slot is executed multiple times (Qt 5.15.5).
-
Hi. I am trying to make a simple connection between C++ and QML through signals and slots. Everything works correctly except for one thing:
I have the C++ signal, which is emitted like this:
emit mySignal();
And in my
test.qml
file, I have the corresponding slot:Connections{ target: mainWindow function onMySignal(){ console.log("Signal received"); } }
This whole process is handled automatically by the QML engine.
When the signal is first emitted, the operation is correct:
"Signal received"
appears once. However, when I emit the signal more times, the number of slot executions increases for each time the signal is emitted, ie:First emit: slot is executed once.
Second emit: slot is executed twice.
Third emit: slot is executed three times.
And so on.The desired behaviour would be that, each time I make an emit, the slot is executed only once. How can I achieve this?
Thanks in advance.
-
@SGaist Thanks again for your reply, it really gave me the key to know what was going on. The problem was that the
Connections
were defined in a component that was continuously being created and destroyed, as it is part of aStackView
. With this behaviour, every time the component was created, a new connection was defined. Moving the connections to a general component solves the problem.Thank you very much! :)
-
-
@SGaist Thanks again for your reply, it really gave me the key to know what was going on. The problem was that the
Connections
were defined in a component that was continuously being created and destroyed, as it is part of aStackView
. With this behaviour, every time the component was created, a new connection was defined. Moving the connections to a general component solves the problem.Thank you very much! :)
-