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.
-
émit
does not create any new connection. The slot is called because the connection was created.Ensure that you don't have multiple objects created that are connected to your C++ object.
@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! :)
-
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.
Hi,
It means that you are connecting the signal more than once. Check your code for that.
-
@SGaist Thank you for your response. In fact, I am not making any connect because this part is already handled by the QML engine. Is it possible to somehow get the QML engine not to create a new connection every time an emit is made?
émit
does not create any new connection. The slot is called because the connection was created.Ensure that you don't have multiple objects created that are connected to your C++ object.
-
émit
does not create any new connection. The slot is called because the connection was created.Ensure that you don't have multiple objects created that are connected to your C++ object.
@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! :)
-