[Qt-interest] Qt State machine framework

K. Frank kfrank29.c at gmail.com
Fri Mar 12 15:41:30 CET 2010


Hi -

On Fri, Mar 12, 2010 at 8:11 AM, Mandeep Sandhu
<mandeepsandhu.chd at gmail.com> wrote:
> On Fri, Mar 12, 2010 at 6:34 PM, Mandeep Sandhu
> <mandeepsandhu.chd at gmail.com> 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.
>> ...

You may be able to use a history pseudo-state:  QHistoryState.

(Warning, I have never used the Qt state-machine framework, so this is
speculation on what might work, and I don't know the Qt details.)

Qt claims to implement hierarchical state machines (HSM's, sometimes
called "statecharts"), a useful feature of which is the history mechanism.
It sounds like this is exactly what you're looking for.  Perhaps the section
"Using History States to Save and Restore the Current State" of the Qt
state-machine documentation, statemachine-api.html, could show you
the Qt / HSM way of doing this.

>> ...
>> Any thoughts?
>
> I do! :)
> ...
> When I have to exit s4, I can check some var that I have set (which
> stores the state from which s4 was entered) and do a
> postEvent(e1/e2/e3) to transition back to the original state.
>
> Or is there a better way?
> ...

Yes, as mentioned above, the history functionality of HSM's might be a
better way.

A philosophical aside...

A key idea of the state-machine programming paradigm is that the state
of your state machine (or program, or object, or whatever) is specified
(almost) entirely by the state (in the state-machine sense) it is in, rather
than by the values of data members (or worse yet, where you are in some
nested if-then-else construct).  This can be a very powerful concept for
organizing your code.

However, it was recognized early on that certain common requirements, in
particular history, don't fit the "pure" state-machine paradigm.  This was
one of the drivers for the development of HSM's.  So rather than subvert the
state-machine paradigm on your own (by adding member variables that contain
additional "state" of your system, such as the previous state), you might want
to use the built-in "escape valve" provided by the history mechanism.

Good luck.


K. Frank

>>
>> Thanks,
>> -mandeep
>> ...



More information about the Qt-interest-old mailing list