The QT state machine framework is implemented based on the state diagram XML (scxml). Starting with Qt4.6, it is already part of the Qtcore module. Although it is a complex set of things in itself, it is not a high threshold to be used after a deep integration with the QT event System, the signal slot (signals and slots), and the property system.
Some concepts
QT's Handbook, the state Machine framework article, describes the framework and use of QT states, but it is also helpful to see the basic concepts (see Scxml's third part).
Basic concepts
The most basic concepts are:
- Status (state)
- Transformation? Transition? (transition)
- Event (events)
Each state contains a collection of transition that define how to respond to events. Events can be generated by the state machine itself or by an external entity.
In a classic state machine, the state machine is always in a single states. This status is called Active state. When the event occurs, the state machine checks all the transition that are contained by active states. If it finds one that matches the event, the state machine will move from the current active to the transition-specified (called the transtion target). As a result, the target State becomes the new active state.
In the process of transition, the state machine can perform some action (action). Each state can contain onentry and onexit actions, and transition itself can also contain actions. When the state machine transitions from state S1 to S2 through a transition called T, it performs the onexit action of S1 first, then executes the actions contained in T itself, and finally executes the S2 onentry action.
Compound states
A state can also nest other state. Such a state is called the compound states (compound status), which we call a parent, and nested is called a child. The child state can also nest other state, until any depth. A state that does not contain any of the child states, known as the Atomic (atomic status).
When a child state is in Acitve, its parent active must also be active. In this way, at any point we will have a collection of active state containing the atomic state and all its ancestors. (We'll see that multiple atoms state can be active at the same time).
Because of the presence of the composite state, the transition will no longer be transformed from one atom state to another, but rather from one active state to another collection. If the target of the transition is an atomic state, then the status machine will not only enter into that atom, but will also enter all of its active ancestor states. In response, transition's goal is a combined state. In this case, the child state of the composite state must also be activated, since transition does not specify which one, which is required to be active in the initial states of the composite (initial status). If the state is still compound, it will recursively go down until the atom states.
A composite state can also contain final and history state as its child state.
The composite state also affects the selection of transition. When an event occurs, the state machine begins its search from the deepest, nested states (atomic state), and if no matching transition is found, it finds the transition of its parent, recursively. If all transition in the state machine do not match, the event is discarded.
Parallel states
Note: In Scxml Parallel states is using the paralled tag, and the previously mentioned composite state and atomic state are all using the State label. In Qt, the paralled state and the normal state are distinguished by a parameter of the constructor function.
The Parallel states (parallel state) is very different from the composite states described earlier: When a composite state is active, and only one of the child states is in acitve, while the Parallel state is active, All child states must be in the active state.
When the state machine goes into the parallel states, it also enters the individual child. Each child state can take a different transition to respond to the event.
Executable Content
Scxml through executable content (executable contents?) Provides the ability to modify the data model and interact with external entities.
Back to Qt
This part of the content of QT manual is too detailed to know how to write down.
Basic concepts
3 Basic Concept Controls:
State |
Qabstractstate and its derived classes |
Transition |
Qabstracttransition and its derived classes |
Event |
QT Signals and events |
Data |
QT Properties |
Note: The qstatemachine itself is a derived class of qstate. This allows the state machine to be nested, see Qstatemachines-a-state-too.
- Qobject
- Qabstractstate
- Qstate
- Qfinialstate
- Qhistorystate
- Qabstracttransition
- Qsignaltransition
- Qeventtransition
- Qkeyeventtransition
- Qmouseeventtransition
- Qevent
Triggering of the transition
QT signals and events can trigger transition. So how is it implemented?
Open Qabstracttransition's manual, you can see two protected functions:
virtual bool eventtest (qevent * Event) = 0virtual void ontransition (qevent * Event) = 0
The former is used to determine if the event matches and the latter is used to perform some actions.
The derived class relationships of two concrete classes and corresponding qevent are as follows:
Qsignaltransition |
Qstatemachine::signalevent |
Qeventtransition |
Qstatemachine::wrappedevent |
After entering the state, the state machine registers the corresponding transition of the active states:
For the signal type, connect the signal to a private slot function that generates an internal qstatemachine::signalevent event in the slot function
For the event type, the event filter is installed directly on the object's objects, and the event is copied and encapsulated into an internal qstatemachine::wrappedevent event.
For custom events, you can use Qstatemachine::p ostevent () to distribute.
Reference
Http://doc.qt.nokia.com/4.7/statemachine-api.html
http://www.w3.org/TR/scxml/
http://labs.qt.nokia.com/2009/07/23/qstatemachines-a-state-too/
http://labs.qt.nokia.com/2009/08/10/introducing-scc-the-scxml-compiler-for-the-qt-state-machine-framework/
http://labs.qt.nokia.com/2009/11/09/about-dynamic-ui-web-apps-performance-and-state-machines/
http://labs.qt.nokia.com/2009/01/30/qt-state-machine-framework/
http://blog.csdn.net/dbzhang800/article/details/6408008
Qt State Machine Framework Learning (not learned)