Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ signal is emitted only once, but the QML slot is executed multiple times (Qt 5.15.5).

C++ signal is emitted only once, but the QML slot is executed multiple times (Qt 5.15.5).

Scheduled Pinned Locked Moved Solved QML and Qt Quick
signals & slotssignals emitconnections
5 Posts 2 Posters 929 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexuds79
    wrote on 10 Jul 2023, 19:09 last edited by
    #1

    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.

    S 1 Reply Last reply 10 Jul 2023, 19:16
    0
    • S SGaist
      10 Jul 2023, 19:24

      é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.

      A Offline
      A Offline
      Alexuds79
      wrote on 10 Jul 2023, 19:44 last edited by
      #5

      @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 a StackView. 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! :)

      1 Reply Last reply
      1
      • A Alexuds79
        10 Jul 2023, 19:09

        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.

        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Jul 2023, 19:16 last edited by
        #2

        Hi,

        It means that you are connecting the signal more than once. Check your code for that.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 10 Jul 2023, 19:21
        2
        • S SGaist
          10 Jul 2023, 19:16

          Hi,

          It means that you are connecting the signal more than once. Check your code for that.

          A Offline
          A Offline
          Alexuds79
          wrote on 10 Jul 2023, 19:21 last edited by
          #3

          @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?

          S 1 Reply Last reply 10 Jul 2023, 19:24
          0
          • A Alexuds79
            10 Jul 2023, 19:21

            @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?

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Jul 2023, 19:24 last edited by
            #4

            é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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply 10 Jul 2023, 19:44
            1
            • S SGaist
              10 Jul 2023, 19:24

              é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.

              A Offline
              A Offline
              Alexuds79
              wrote on 10 Jul 2023, 19:44 last edited by
              #5

              @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 a StackView. 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! :)

              1 Reply Last reply
              1
              • A Alexuds79 has marked this topic as solved on 10 Jul 2023, 21:52

              3/5

              10 Jul 2023, 19:21

              • Login

              • Login or register to search.
              3 out of 5
              • First post
                3/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved