Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to access the signal parameters associated with the QSignalTransition in a lambda
QtWS25 Last Chance

how to access the signal parameters associated with the QSignalTransition in a lambda

Scheduled Pinned Locked Moved Solved General and Desktop
lamdaqstatemachinesignal & slot
9 Posts 2 Posters 3.1k 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
    alex_qwa
    wrote on last edited by
    #1

    Hello every one,

    I've created a custom transition in the QStatemachine framwork as follows:

    QSignalTransition *trans = new QSignalTransition(&myObj, SIGNAL(notifyMySignalWParam(QByteArray), s1);
    s1->addTransition(trans);
    connect(trans, &QSignalTransition::triggered, [=](){.......});
    

    I want to know how I can access the parameter(s) of the signal (in this case the QByteArray) in the lambda?

    Any help is highly appreciated.

    VRoninV 1 Reply Last reply
    0
    • A alex_qwa

      Hello every one,

      I've created a custom transition in the QStatemachine framwork as follows:

      QSignalTransition *trans = new QSignalTransition(&myObj, SIGNAL(notifyMySignalWParam(QByteArray), s1);
      s1->addTransition(trans);
      connect(trans, &QSignalTransition::triggered, [=](){.......});
      

      I want to know how I can access the parameter(s) of the signal (in this case the QByteArray) in the lambda?

      Any help is highly appreciated.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @alex_qwa Can't you connect the lambda to myObj::notifyMySignalWParam?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      A 1 Reply Last reply
      0
      • VRoninV VRonin

        @alex_qwa Can't you connect the lambda to myObj::notifyMySignalWParam?

        A Offline
        A Offline
        alex_qwa
        wrote on last edited by VRonin
        #3

        @VRonin thanks for the reply; if i make the connection to ```

        myObj::notifyMySignalWParam
        

        directly then the slot/lambda will be called whenever the signal is emitted which is not what i want. We are in a FSM and the "transition" is supposed to be fired only when the signal is fired in the "s1" state of the statemachine.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          QSignalTransition does not store that argument at all. You can work around it like this:

          // I assume myObj is of type MyObject
          QSignalTransition *trans = new QSignalTransition(&myObj, SIGNAL(notifyMySignalWParam(QByteArray), s1);
          s1->addTransition(trans);
          QByteArray* argumentHolder = new QByteArray();
          connect(&myObj,&MyObject::notifyMySignalWParam,[=](const QByteArray& byArrArg)->void{*argumentHolder = byArrArg;});
          connect(trans, &QSignalTransition::triggered, [=](){
          doSomethingWithTheArguments(argumentHolder);
          delete argumentHolder;
          });
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          A 1 Reply Last reply
          1
          • VRoninV VRonin

            QSignalTransition does not store that argument at all. You can work around it like this:

            // I assume myObj is of type MyObject
            QSignalTransition *trans = new QSignalTransition(&myObj, SIGNAL(notifyMySignalWParam(QByteArray), s1);
            s1->addTransition(trans);
            QByteArray* argumentHolder = new QByteArray();
            connect(&myObj,&MyObject::notifyMySignalWParam,[=](const QByteArray& byArrArg)->void{*argumentHolder = byArrArg;});
            connect(trans, &QSignalTransition::triggered, [=](){
            doSomethingWithTheArguments(argumentHolder);
            delete argumentHolder;
            });
            
            A Offline
            A Offline
            alex_qwa
            wrote on last edited by
            #5

            @VRonin, The signal might be transmitted many times, so maybe we should initialize the pointer to the argument holder in the lambda??

            connect(&myObj,&MyObject::notifyMySignalWParam,[=](const QByteArray& byArrArg)->void{argumentHolder = new QByteArray(); *argumentHolder = byArrArg;});
            

            Isn't there any other way, for executing an action in a "state" without making a transition out of that state when a specific "signal" is fired?? (that is actually what i want to achieve, if it is not clear from the code).

            Thanks again for the input.

            VRoninV 1 Reply Last reply
            0
            • A alex_qwa

              @VRonin, The signal might be transmitted many times, so maybe we should initialize the pointer to the argument holder in the lambda??

              connect(&myObj,&MyObject::notifyMySignalWParam,[=](const QByteArray& byArrArg)->void{argumentHolder = new QByteArray(); *argumentHolder = byArrArg;});
              

              Isn't there any other way, for executing an action in a "state" without making a transition out of that state when a specific "signal" is fired?? (that is actually what i want to achieve, if it is not clear from the code).

              Thanks again for the input.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @alex_qwa said in how to access the signal parameters associated with the QSignalTransition in a lambda:

              so maybe we should initialize the pointer to the argument holder in the lambda??

              No, argumentHolder = new QByteArray(); assigns to argumentHolder that is passed by value, the change will die in the scope of the assignment and leak memory if called more than once.

              executing an action in a "state" without making a transition out of that state when a specific "signal" is fired

              Can you elaborate better the question? maybe with a tiny example?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              A 1 Reply Last reply
              0
              • VRoninV VRonin

                @alex_qwa said in how to access the signal parameters associated with the QSignalTransition in a lambda:

                so maybe we should initialize the pointer to the argument holder in the lambda??

                No, argumentHolder = new QByteArray(); assigns to argumentHolder that is passed by value, the change will die in the scope of the assignment and leak memory if called more than once.

                executing an action in a "state" without making a transition out of that state when a specific "signal" is fired

                Can you elaborate better the question? maybe with a tiny example?

                A Offline
                A Offline
                alex_qwa
                wrote on last edited by
                #7

                @VRonin regarding the "argument holder": when the signal is fired for the first time the pointer is "deleted"; won't this cause problems when the signal is fired again?

                Regarding the example: actually mycode isvery similar to the code in Qt help explaining the Qt Statemachine framework: "Targetless Transitions".

                VRoninV 1 Reply Last reply
                0
                • A alex_qwa

                  @VRonin regarding the "argument holder": when the signal is fired for the first time the pointer is "deleted"; won't this cause problems when the signal is fired again?

                  Regarding the example: actually mycode isvery similar to the code in Qt help explaining the Qt Statemachine framework: "Targetless Transitions".

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @alex_qwa said in how to access the signal parameters associated with the QSignalTransition in a lambda:

                  won't this cause problems when the signal is fired again?

                  No, the memory allocated to the pointer will remain valid

                  Regarding the example: actually mycode isvery similar to the code in Qt help explaining the Qt Statemachine framework: "Targetless Transitions".

                  Yes, what I mean is that you probably don't have to mess with states at all. If I'm getting this correctly, you want notifyMySignalWParam to trigger something but only if you are in state s1. Correct?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  A 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @alex_qwa said in how to access the signal parameters associated with the QSignalTransition in a lambda:

                    won't this cause problems when the signal is fired again?

                    No, the memory allocated to the pointer will remain valid

                    Regarding the example: actually mycode isvery similar to the code in Qt help explaining the Qt Statemachine framework: "Targetless Transitions".

                    Yes, what I mean is that you probably don't have to mess with states at all. If I'm getting this correctly, you want notifyMySignalWParam to trigger something but only if you are in state s1. Correct?

                    A Offline
                    A Offline
                    alex_qwa
                    wrote on last edited by alex_qwa
                    #9

                    @VRonin : yes, but the system is rather complex and i want to use the statemachine framwork to implement it).
                    By the way this same signal might require other actions in some of the other states
                    Thanks a lot for your time

                    1 Reply Last reply
                    0

                    • Login

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