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



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


  • Qt Champions 2018

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



  • @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.


  • Qt Champions 2018

    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;
    });
    


  • @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.


  • Qt Champions 2018

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



  • @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".


  • Qt Champions 2018

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



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