Provides a hierarchical finite state machine. More...
Import Statement: | import QtQml.StateMachine 6.4 |
Since: | Qt 5.4 |
Inherits: |
StateMachine is based on the concepts and notation of Statecharts. StateMachine is part of Qt State Machine QML API
A state machine manages a set of states and transitions between those states; these states and transitions define a state graph. Once a state graph has been built, the state machine can execute it. StateMachine's execution algorithm is based on the State Chart XML (SCXML) algorithm. The framework's overview gives several state graphs and the code to build them.
Before the machine can be started, the initialState must be set. The initial state is the state that the machine enters when started. You can then set running property to true or start() the state machine. The started signal is emitted when the initial state is entered.
The state machine processes events and takes transitions until a top-level final state is entered; the state machine then emits the finished() signal. You can also stop() the state machine explicitly (you can also set running property to false). The stopped signal is emitted in this case.
The following snippet shows a state machine that will finish when a button is clicked:
import QtQuick import QtQml.StateMachine as DSM Rectangle { Button { anchors.fill: parent id: button text: "Finish state" DSM.StateMachine { id: stateMachine initialState: state running: true DSM.State { id: state DSM.SignalTransition { targetState: finalState signal: button.clicked } } DSM.FinalState { id: finalState } onFinished: Qt.quit() } } }
If an error is encountered, the machine will look for an errorState, and if one is available, it will enter this state. After the error state is entered, the type of the error can be retrieved with error(). 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.
Warning: Setting the childMode of a StateMachine to anything else than QState::ExclusiveStates will result in an invalid state machine, and can lead to incorrect behavior.
See also QAbstractState, State, SignalTransition, TimeoutTransition, HistoryState, and Qt State Machine QML Guide.
[read-only] errorString : string |
The error string of this state machine.
globalRestorePolicy : enumeration |
The restore policy for states of this state machine.
The default value of this property is QState.DontRestoreProperties.
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 QState.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.
started() |
This signal is emitted when the state machine has entered its initial state (State::initialState).
Note: The corresponding handler is onStarted
.
See also running, start(), and State::finished.
stopped() |
This signal is emitted when the state machine has stopped.
Note: The corresponding handler is onStopped
.
See also running, stop(), and State::finished.
start() |
Starts this state machine. The machine will reset its configuration and transition to the initial state. When a final top-level state (FinalState) is entered, the machine will emit the finished() signal.
Note: A state machine will not run without a running event loop, such as the main application event loop started with QCoreApplication::exec() or QApplication::exec().
See also started, State::finished, stop(), State::initialState, and running.
stop() |