
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQStateMachine class provides a hierarchical finite state machine.More...
| Header: | #include <QStateMachine> |
| Since: | Qt 4.6 |
| Inherits: | QState |
Note: All functions in this class arereentrant, butpostEvent(),postDelayedEvent(), andcancelDelayedEvent() are alsothread-safe.
| class | SignalEvent |
| class | WrappedEvent |
| enum | Error { NoError, NoInitialStateError, NoDefaultStateInHistoryStateError, NoCommonAncestorForTransitionError } |
| enum | EventPriority { NormalPriority, HighPriority } |
| enum | RestorePolicy { DontRestoreProperties, RestoreProperties } |
| QStateMachine(QObject * parent = 0) | |
| ~QStateMachine() | |
| void | addDefaultAnimation(QAbstractAnimation * animation) |
| void | addState(QAbstractState * state) |
| bool | cancelDelayedEvent(int id) |
| void | clearError() |
| QSet<QAbstractState *> | configuration() const |
| QList<QAbstractAnimation *> | defaultAnimations() const |
| Error | error() const |
| QString | errorString() const |
| QStateMachine::RestorePolicy | globalRestorePolicy() const |
| bool | isAnimated() const |
| bool | isRunning() const |
| int | postDelayedEvent(QEvent * event, int delay) |
| void | postEvent(QEvent * event, EventPriority priority = NormalPriority) |
| void | removeDefaultAnimation(QAbstractAnimation * animation) |
| void | removeState(QAbstractState * state) |
| void | setAnimated(bool enabled) |
| void | setGlobalRestorePolicy(QStateMachine::RestorePolicy restorePolicy) |
| virtual bool | eventFilter(QObject * watched, QEvent * event) |
| virtual bool | event(QEvent * e) |
| virtual void | onEntry(QEvent * event) |
| virtual void | onExit(QEvent * event) |
TheQStateMachine class provides a hierarchical finite state machine.
QStateMachine is based on the concepts and notation ofStatecharts.QStateMachine is part ofThe State Machine Framework.
A state machine manages a set of states (classes that inherit fromQAbstractState) and transitions (descendants ofQAbstractTransition) between those states; these states and transitions define a state graph. Once a state graph has been built, the state machine can execute it.QStateMachine's execution algorithm is based on theState Chart XML (SCXML) algorithm. The framework'soverview gives several state graphs and the code to build them.
Use theaddState() function to add a top-level state to the state machine. States are removed with theremoveState() function. Removing states while the machine is running is discouraged.
Before the machine can be started, theinitial state must be set. The initial state is the state that the machine enters when started. You can thenstart() the state machine. Thestarted() signal is emitted when the initial state is entered.
The machine is event driven and keeps its own event loop. Events are posted to the machine throughpostEvent(). Note that this means that it executes asynchronously, and that it will not progress without a running event loop. You will normally not have to post events to the machine directly as Qt's transitions, e.g.,QEventTransition and its subclasses, handle this. But for custom transitions triggered by events,postEvent() is useful.
The state machine processes events and takes transitions until a top-level final state is entered; the state machine then emits thefinished() signal. You can alsostop() the state machine explicitly. Thestopped() signal is emitted in this case.
The following snippet shows a state machine that will finish when a button is clicked:
QPushButton button;QStateMachine machine;QState*s1=newQState();s1->assignProperty(&button,"text","Click me");QFinalState*s2=newQFinalState();s1->addTransition(&button, SIGNAL(clicked()), s2);machine.addState(s1);machine.addState(s2);machine.setInitialState(s1);machine.start();
This code example usesQState, which inheritsQAbstractState. TheQState class provides a state that you can use to set properties and invoke methods onQObjects when the state is entered or exited. It also contains convenience functions for adding transitions, e.g.,QSignalTransitions as in this example. See theQState class description for further details.
If an error is encountered, the machine will look for anerror state, and if one is available, it will enter this state. The types of errors possible are described by theError enum. After the error state is entered, the type of the error can be retrieved witherror(). The execution of the state graph will not stop when the error state is entered. If no error state applies to the erroneous state, the machine will stop executing and an error message will be printed to the console.
See alsoQAbstractState,QAbstractTransition,QState, andThe State Machine Framework.
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 byerror(), the error message returned byerrorString(), and enter an error state based on the context of the error.
| Constant | Value | Description |
|---|---|---|
QStateMachine::NoError | 0 | No error has occurred. |
QStateMachine::NoInitialStateError | 1 | The machine has entered aQState with children which does not have an initial state set. The context of this error is the state which is missing an initial state. |
QStateMachine::NoDefaultStateInHistoryStateError | 2 | The machine has entered aQHistoryState which does not have a default state set. The context of this error is theQHistoryState which is missing a default state. |
QStateMachine::NoCommonAncestorForTransitionError | 3 | The machine has selected a transition whose source and targets are not part of the same tree of states, and thus are not part of the same state machine. Commonly, this could mean that one of the states has not been given any parent or added to any machine. The context of this error is the source state of the transition. |
See alsosetErrorState().
This enum type specifies the priority of an event posted to the state machine usingpostEvent().
Events of high priority are processed before events of normal priority.
| Constant | Value | Description |
|---|---|---|
QStateMachine::NormalPriority | 0 | The event has normal priority. |
QStateMachine::HighPriority | 1 | The event has high priority. |
This enum specifies the restore policy type. The restore policy takes effect when the machine enters a state which sets one or more properties. If the restore policy is set to RestoreProperties, the state machine will save the original value of the property before the new value is set.
Later, when the machine either enters a state which does not set a value for the given property, the property will automatically be restored to its initial value.
Only one initial value will be saved for any given property. If a value for a property has already been saved by the state machine, it will not be overwritten until the property has been successfully restored.
| Constant | Value | Description |
|---|---|---|
QStateMachine::DontRestoreProperties | 0 | The state machine should not save the initial values of properties and restore them later. |
QStateMachine::RestoreProperties | 1 | The state machine should save the initial values of properties and restore them later. |
See alsoQStateMachine::globalRestorePolicy andQState::assignProperty().
This property holds whether animations are enabled.
The default value of this property is true.
Access functions:
| bool | isAnimated() const |
| void | setAnimated(bool enabled) |
See alsoQAbstractTransition::addAnimation().
This property holds the error string of this state machine.
Access functions:
| QString | errorString() const |
This property holds the restore policy for states of this state machine.
The default value of this property isQStateMachine::DontRestoreProperties.
Access functions:
| QStateMachine::RestorePolicy | globalRestorePolicy() const |
| void | setGlobalRestorePolicy(QStateMachine::RestorePolicy restorePolicy) |
Constructs a new state machine with the givenparent.
Destroys this state machine.
Adds a defaultanimation to be considered for any transition.
Adds the givenstate to this state machine. The state becomes a top-level state.
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 alsoremoveState() andsetInitialState().
Cancels the delayed event identified by the givenid. The id should be a value returned by a call topostDelayedEvent(). Returns true if the event was successfully cancelled, otherwise returns false.
Note: This function isthread-safe.
See alsopostDelayedEvent().
Clears the error string and error code of the state machine.
Returns the maximal consistent set of states (including parallel and final states) that this state machine is currently in. If a states is in the configuration, it is always the case that the parent ofs is also in c. Note, however, that the machine itself is not an explicit member of the configuration.
Returns the list of default animations that will be considered for any transition.
Returns the error code of the last error that occurred in the state machine.
[virtual protected]bool QStateMachine::event(QEvent * e)Reimplemented fromQObject::event().
[virtual]bool QStateMachine::eventFilter(QObject * watched,QEvent * event)Reimplemented fromQObject::eventFilter().
Returns whether this state machine is running.
[virtual protected]void QStateMachine::onEntry(QEvent * event)Reimplemented fromQAbstractState::onEntry().
This function will callstart() to start the state machine.
[virtual protected]void QStateMachine::onExit(QEvent * event)Reimplemented fromQAbstractState::onExit().
This function will callstop() to stop the state machine and subsequently emit thestopped() signal.
Posts the givenevent for processing by this state machine, with the givendelay in milliseconds. Returns an identifier associated with the delayed event, or -1 if the event could not be posted.
This function returns immediately. When the delay has expired, the event will be added to the state machine's event queue for processing. 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.
Note: This function isthread-safe.
See alsocancelDelayedEvent() andpostEvent().
Posts the givenevent of the givenpriority for processing by this state machine.
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.
Note: This function isthread-safe.
See alsopostDelayedEvent().
Removesanimation from the list of default animations.
Removes the givenstate from this state machine. The state machine releases ownership of the state.
See alsoaddState().
[slot]void QStateMachine::start()Starts this state machine. The machine will reset its configuration and transition to the initial state. When a final top-level state (QFinalState) is entered, the machine will emit thefinished() signal.
Note:A state machine will not run without a running event loop, such as the main application event loop started withQCoreApplication::exec() orQApplication::exec().
See alsostarted(),finished(),stop(), andinitialState().
[signal]void QStateMachine::started()This signal is emitted when the state machine has entered its initial state (QStateMachine::initialState).
See alsoQStateMachine::finished() andQStateMachine::start().
[slot]void QStateMachine::stop()Stops this state machine. The state machine will stop processing events and then emit thestopped() signal.
[signal]void QStateMachine::stopped()This signal is emitted when the state machine has stopped.
See alsoQStateMachine::stop() andQStateMachine::finished().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.