[Qt-interest] QStateMachine API and design?

Sean Harmer sean.harmer at maps-technology.com
Thu Nov 4 14:09:10 CET 2010


Hi Steve,

On Thursday 04 November 2010 12:26:00 Stephen Kelly wrote:
> After figuring out how to use QStateMachine to do what I want I have some
> questions. I'm sure it's all deliberate, but I can't figure out why some
> things are the way they are.
> 
> * Why is there no 'current state'? There is a configuration() which gives
> me a set of QStates, but it doesn't tell me which one is the current one.
> To get that I iterate over the set and analyse the parent() until I get
> the 'most extended' state.

I thnk you may have a slight conceptual misunderstanding here. Th 
configuration is the set of current states. You do not normally have just a 
single current state. When a child state is active ten so is it's parent state 
and it's parent and so on.

For example, imagine a piece of hardware that is modelled and controlled by a 
SM. This may have states like this:

Closed
Open
 Initialising
 Initialised
  Idle
  Busy doing task 1
  Busy doing task 2

Now if the Idle state is in the current configuration we must also be in the 
"Initialised" and "Open" states too. 

Nested substates "inherit" the behaviour of their parent states. This is very 
handy indeed if you need to handle transitions from all child states within a 
parent state, you simply put the transition on the parent. However this 
transition would not work if the parent state was not in the current 
configuration - only transitions that have source states in the current 
configuration are checked as possibilities when an event is processed b ythe 
SM.

I can see that you may expect this behaviour to be possible if only the 
deepest state was listed in the current configuration. However, it gets 
tricker when you start to consider SM's with parallel states in addition to 
the more usual mutually exclusive states.

> * Why is there no notification when the 'current state' is changed?

I have filed a bug for this after discussion with Kent:

http://bugreports.qt.nokia.com/browse/QTBUG-9380

Please vote for it or file a MR. It should be asimple change and it is on my 
list but time as always is short. A graphical SM debugger would also be *very* 
nice but that's a much larger task.

> My usecase is to create a state machine and expose it to QML so that I can
> change the ui state on change of the application state. Approximately:
> 
> Item {
>   State {
>     when : application.state == "connecting"
>     PropertyChanges { target : myText; text : "Please wait" }
>   }
> }
> 
> I have that working by wrapping the statemachine and creating a
> NotifyingStateMachine. I just wonder what the right way to do it is and why
> there is no notification in QStateMachine. Am I missing the obvious way to
> make application state available to QML through QStateMachine?

Another approach is to connect the entered()/exited() signals of QState or the 
triggered() signal of the transition. Or you can implement the 
onEntry()/onExit() functions in your own custom states. We have largely gone 
for custom states and used overridden onEntry() functions. Not sure if any of 
these would map cleanly onto a Qml scene though as I have not tried it in 
anger yet.

Good luck,

Sean



More information about the Qt-interest-old mailing list