[Qt-interest] Qt State machine framework

Kent Hansen khansen at trolltech.com
Fri Mar 12 19:02:33 CET 2010


Hi,

Mandeep Sandhu wrote:
> I had one more requirement, which I can't see a clean way to implement...
>
> Say my FSM has 4 states: s1, s2, s3 and s4.
>
> s4 can be entered from either s1, s2 or s3.
>
> When in s4, on an event, I need to go back to the state from which I
> had entered s4.
>
> So the transition from s4 has to take into account from which state it
> was entered so that it can go back to it. I can store the state using
> the onExit() callback....but how do I make it go to the required
> state. I can't use the setTarget() api since this has to be determined
> while the FSM is running and not before when i'm setting up the
> transitions.
>
> Any thoughts?
>   

Like K. Frank suggested, use a history state.

1. Create a group state that has s1, s2, s3, and a history state h1, as 
its children. Create the transitions from s1, s2, s3 to s4 like before.
2. From s4 (which is _not_ a child of the group state from 1) ), add a 
transition with h1 as its target

The state machine algorithm should take care of the rest (actually 
saving / restoring the state).

Kent




More information about the Qt-interest-old mailing list