Easy way to "reuse" transitions from State Machine Framework



  • Hello,

    I'm designing an application in which I make use of the SMF to help synchronize some of the widgets depending on user interaction.

    The problem I have is that there are some situations in which, for a one same action taken by the user (in a particular case, a KeyPress event), I must transition to a particular state A if I'm in one of the {B,C,D,F} states (state names for example purpose).

    So, what I have to do is add, for each of the states in {B,C,D,F}, one QEventTransition of type QEvent::KeyPress, all of them having the same destination. But as the states claim ownership of transitions, I cannot new up a single QEventTransition and pass it to those states, forcing me to replicate the code several times.

    Is this really the way? Isn't there any way to reuse a transition, or even "clone" one to avoid so much retyping?

    QEventTransition *t_maybeNewEntry = Q_NULLPTR;
    t_maybeNewEntry = new QEventTransition(
                    mainWindow.ui->lineEditOutput,
                    QEvent::KeyPress
                    );
    t_maybeNewEntry->setTargetState(s_hasEntry);
    //...
    t_maybeNewEntry = new QEventTransition(
                    mainWindow.ui->lineEditOutput,
                    QEvent::KeyPress
                    );
    t_maybeNewEntry->setTargetState(s_hasEntry);
    //...
    t_maybeNewEntry = new QEventTransition(
                    mainWindow.ui->lineEditOutput,
                    QEvent::KeyPress
                    );
    t_maybeNewEntry->setTargetState(s_hasEntry);
    //... ... ...
    

    Thank you.



  • Have a look at the State Machine Framework documentation here.

    Basically make one state with your defined transition, and create your other states accordingly with the parent state that has your single transition defined.

    Good luck.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.