View Full Version : State Machine returning to previous state

7th May 2011, 17:26
I've got a state machine, but I need a bit complexer behavior than the default: when I enter a particular state, some times I need to have a regular transition to another state. Other times I need to return to previous state.

For example, having states A B C, say that transition T moves state from A to C and from B to C. I need that a transition S moves from C to A when T occurred in state A and from C to B when it occurred in state B.

As you can see, this is a bit outside of the definition of finite state machine, as this is stack based and therefore needs "memory".

I'm aware of the QHistoryState, but the documentation says it's made when moving outside the "level" of the current state (edit: precisely it says "A history state is a pseudo-state that represents the child state that the parent state was in the last time the parent state was exited"), while in my case, A, B and C are on the same level and as matter of facts it doesn't work :D

Is there something in State Machine Framework that allows me to do this? Maybe subclassing QStateMachine or implementing a custom QAbstractState that may be similar to QHistoryState?

I've some ideas on how to do this, but I'm hoping, of course, for an existing Qt solution.

Thanks in advance

8th May 2011, 11:12
My best guess would be to redesign your state machine so that A and B are substates of a new state X. Then C would become a kind of "external" state to your main state machine (formed from A, B and possibly some other states you might want to distinguish in future).

10th May 2011, 01:39
Initially this wasn't an option, but I re-worked the state logic and managed to make it in that way to avoid writing custom classes.
Thanks for the suggestion!