A state machine is a model of behavior composed of a number of states, transitions between those states, and actions. A state machine is an abstract model of a machine with a primitive internal memory.
The state machine framework provides a robust state chart implementation based on Harel statecharts and SCXML.
2. What's New in Qt 4.6
Animation Framework
State Machine Framework
Multi-touch & Gestures
DOM access API
Qt3D enablers
Performance Optimizations
Multimedia Audio Services
Link
3. Animation Framework
The animation framework helps build highly animated, high-performance GUIs
without the hassle of managing complex structures, timers, and easing curves,
not to mention the large state graphs that all animated GUIs tend to be full of.
The animation framework also plugs into the new Qt Statemachine by allowing an
animation to be played when transitions are triggered. The state machine
framework is introduced in 4.6 and is described below.
4. State Machine
A state machine is a model of behavior
composed of a number of states, transitions
between those states, and actions. A state
machine is an abstract model of a machine
with a primitive internal memory.
Wikipedia: Finite-state machine
5. State Machine Framework
The state machine framework provides a robust state chart implementation based
on Harel statecharts and SCXML. Qt's API lets you construct such state graphs
and execute them. The key benefits of a state machine are:
Simplify complex application semantics.
Use of states to reduce code bloat.
Use states to improve maintainability.
Makes event-driven programming robust and more reusable.
It is especially the last item here that makes using a state machine worthwhile. A
key characteristic of event-driven systems (such as Qt applications) is that
behavior often depends not only on the last or current event, but also the events
that preceded it. With statecharts, this information is easy to express.
The framework fits neatly into Qt by allowing transitions to trigger on signals and
QEvents. By inserting animations into the state machine, it is also easier to use
the framework for animating GUIs, for instance.
6. Classes in the State Machine Framework
QAbstractState - The base class of states of a QStateMachine
QAbstractTransition - The base class of transitions between QAbstractState
objects
QEventTransition - QObject-specific transition for Qt events
QFinalState - Final state
QHistoryState - Means of returning to a previously active substate
QKeyEventTransition - Transition for key events
QMouseEventTransition - Transition for mouse events
QSignalEvent - Represents a Qt signal event
QSignalTransition - Transition based on a Qt signal
QState - General-purpose state for QStateMachine
QStateMachine - Hierarchical finite state machine
QWrappedEvent - Holds a clone of an event associated with a QObject
7. A Simple State Machine
A state machine with three states, s1, s2 and s3. The state machine is controlled
by a single QPushButton; when the button is clicked, the machine transitions to
another state. Initially, the state machine is in state s1.
First, we create the state machine and states:
QStateMachine machine;
QState *s1 = new QState();
QState *s2 = new QState();
QState *s3 = new QState();
Then, we create the transitions by using
the QState::addTransition() function:
s1->addTransition(button, SIGNAL(clicked()), s2);
s2->addTransition(button, SIGNAL(clicked()), s3);
s3->addTransition(button, SIGNAL(clicked()), s1);
Next, we add the states to the machine and set the machine's initial state:
machine.addState(s1);
machine.addState(s2);
machine.addState(s3);
machine.setInitialState(s1);
Finally, we start the state machine:
machine.start();
The state machine executes asynchronously, i.e. it becomes part of your application's
event loop.
8. Doing Useful Work on State Entry and Exit
The QState::assignProperty() function can be used to have a state set a property
of a QObject when the state is entered.
s1->assignProperty(label, "text", "In state s1");
s2->assignProperty(label, "text", "In state s2");
s3->assignProperty(label, "text", "In state s3");
When any of the states is entered, the label's text will be changed accordingly.
The QState::entered() signal is emitted when the state is entered, and the
QState::exited() signal is emitted when the state is exited.
QObject::connect(s3, SIGNAL(entered()), button, SLOT(showMaximized()));
QObject::connect(s3, SIGNAL(exited()), button, SLOT(showMinimized()));
Custom states can reimplement QAbstractState::onEntry() and
QAbstractState::onExit().
9. Animating Property Assignments
The State Machine API connects with the Animation API in Qt to allow
automatically animating properties as they are assigned in states.
QState *s1 = new QState();
QState *s2 = new QState();
s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100));
s1->addTransition(button, SIGNAL(clicked()), s2);
Here we define two states of a user interface. In s1 the button is small, and in s2
it is bigger. If we click the button to transition from s1 to s2, the geometry of the
button will be set immediately when a given state has been entered. If we want
the transition to be smooth, however, all we need to do is make a
QPropertyAnimation and add this to the transition object.
QSignalTransition *transition = s1->addTransition(button, SIGNAL(clicked()), s2);
transition->addAnimation(new QPropertyAnimation(button, "geometry"));
10. QPropertyAnimation.easingCurve
This property holds the easing curve of the animation.
This property defines the easing curve of the animation. By default, a linear
easing curve is used, resulting in linear interpolation. Other curves are provided,
for instance, QEasingCurve::InCirc, which provides a circular entry curve. Another
example is QEasingCurve::InOutElastic, which provides an elastic effect on the
values of the interpolated variant.