Home

QtStateMachine Class Reference
[QtCore module]

The QtStateMachine class provides a hierarchical finite state machine. More...

 #include <QtStateMachine>

Inherits QObject.

Inherited by QtScriptedStateMachine.

Note: All the functions in this class are reentrant.

This class was introduced in qtstatemachine 4.6.

Public Types

Properties

Public Functions

Public Slots

Signals

Additional Inherited Members


Detailed Description

The QtStateMachine class provides a hierarchical finite state machine.

The QtStateMachine class provides a hierarchical finite state machine based on Statecharts concepts and notation. QtStateMachine is part of The State Machine Framework.

A state machine manages a set of states (QtAbstractState objects) and transitions (QtAbstractTransition objects) between those states; the states and the transitions collectively define a state graph. Once a state graph has been defined, the state machine can execute it. QtStateMachine's execution algorithm is based on the State Chart XML (SCXML) algorithm.

The QtState class provides a state that you can use to set properties and invoke methods on QObjects when the state is entered or exited. This is typically used in conjunction with signals; the signals determine the flow of the state graph, whereas the states' property assignments and method invocations are the actions.

Use the addState() function to add a state to the state machine; alternatively, pass the machine's rootState() to the state constructor. Use the removeState() function to remove a state from the state machine.

The following snippet shows a state machine that will finish when a button is clicked:

 QPushButton button;

 QtStateMachine machine;
 QtState *s1 = new QtState();
 s1->assignProperty(&button, "text", "Click me");

 QtFinalState *s2 = new QtFinalState();
 s1->addTransition(&button, SIGNAL(clicked()), s2);

 machine.addState(s1);
 machine.addState(s2);
 machine.setInitialState(s1);
 machine.start();

The setInitialState() function sets the state machine's initial state; this state is entered when the state machine is started.

The start() function starts the state machine. The state machine executes asynchronously, i.e. you need to run an event loop in order for it to make progress. The started() signal is emitted when the state machine has entered the initial state.

The state machine processes events and takes transitions until a top-level final state is entered; the state machine then emits the finished() signal.

The stop() function stops the state machine. The stopped() signal is emitted when the state machine has stopped.

The postEvent() function posts an event to the state machine. This is useful when you are using custom events to trigger transitions.

The rootState() function returns the state machine's root state. All top-level states have the root state as their parent.

See also QtAbstractState and QtAbstractTransition.


Member Type Documentation

enum QtStateMachine::Error

This enum type defines errors that can occur in the state machine at run time. When the state machine encounters an unrecoverable error at run time, it will set the error code returned by error(), the error message returned by errorString(), and enter an error state based on the context of the error.

ConstantValueDescription
QtStateMachine::NoError0No error has occurred.
QtStateMachine::NoInitialStateError1The machine has entered a QtState with children which does not have an initial state set. The context of this error is the state which is missing an initial state.
QtStateMachine::NoDefaultStateInHistoryState2The machine has entered a QtHistoryState which does not have a default state set. The context of this error is the QtHistoryState which is missing a default state.

See also setErrorState().


Property Documentation

errorState : QtAbstractState *

This property holds the error state of this state machine.

Access functions:

errorString : const QString

This property holds the error string of this state machine.

Access functions:

initialState : QtAbstractState *

This property holds the initial state of this state machine.

Access functions:

rootState : QtState * const

This property holds the root state of this state machine.

Access functions:


Member Function Documentation

QtStateMachine::QtStateMachine ( QObject * parent = 0 )

Constructs a new state machine with the given parent.

QtStateMachine::~QtStateMachine ()

Destroys this state machine.

void QtStateMachine::addState ( QtAbstractState * state )

Adds the given state to this state machine. The state becomes a top-level state (i.e. a child of the rootState()).

If the state is already in a different machine, it will first be removed from its old machine, and then added to this machine.

See also removeState() and rootState().

void QtStateMachine::animationsFinished ()   [signal]

This signal is emitted when the state machine has finished playing all animations associated with the latest transition (i.e., all properties have reached their target values).

void QtStateMachine::clearError ()

Clears the error string and error code of the state machine.

Error QtStateMachine::error () const

Returns the error code of the last error that occurred in the state machine.

void QtStateMachine::finished ()   [signal]

This signal is emitted when the state machine has reached a top-level final state.

See also QtStateMachine::started().

QtAbstractState::RestorePolicy QtStateMachine::globalRestorePolicy () const

Returns the global restore policy of the state machine.

See also setGlobalRestorePolicy() and QtActionState::restorePolicy().

void QtStateMachine::postEvent ( QEvent * event, int delay = 0 )

Posts the given event for processing by this state machine, with a delay of delay milliseconds.

This function returns immediately. The event is added to the state machine's event queue. Events are processed in the order posted. The state machine takes ownership of the event and deletes it once it has been processed.

You can only post events when the state machine is running.

void QtStateMachine::removeState ( QtAbstractState * state )

Removes the given state from this state machine. The state machine releases ownership of the state.

See also addState().

void QtStateMachine::setGlobalRestorePolicy ( QtAbstractState::RestorePolicy restorePolicy )

Sets the global restore policy of the state machine to restorePolicy. The default global restore policy is QtAbstractState::DoNotRestoreProperties.

The global restore policy cannot be set to QtAbstractState::GlobalRestorePolicy.

See also globalRestorePolicy() and QtAbstractState::setRestorePolicy().

void QtStateMachine::start ()   [slot]

Starts this state machine. The machine will reset its configuration and transition to the initial state. When a final top-level state is entered, the machine will emit the finished() signal.

See also started(), finished(), stop(), and initialState().

void QtStateMachine::started ()   [signal]

This signal is emitted when the state machine has entered its initial state.

See also QtStateMachine::finished() and QtStateMachine::start().

QList<QtAbstractState *> QtStateMachine::states () const

Returns the list of this state machine's states, or an empty list if the state machine has no states.

void QtStateMachine::stop ()   [slot]

Stops this state machine.

See also stopped().

void QtStateMachine::stopped ()   [signal]

This signal is emitted when the state machine has stopped.

See also QtStateMachine::stop().


Copyright © 2009 Nokia Trademarks
Qt Solutions