Qt Qml state machine framework and qtqml State framework
Qt5.4 introduces the QML state machine framework, similar to the C ++ state machine framework, which allows you to create and execute State Diagrams in applications. To this end, the QtQml. StateMachine module provides related QML types for creating event-driven state machines. These QML types are listed as follows:
StateMachine-State machine manager, which executes Algorithms Based on SCXML (State Chart XML). Before starting a State machine, you must set the initial State, that is, the initialState attribute.
State -- general type in the State machine. Note that the State type is different from that in the QtQuick module. Import the QtQuick module before importing the QtQml. StateMachine module.
FinalState -- as the name implies, the finished () signal of the state machine is triggered when the state machine stops working.
HistoryState is a pseudo State that records the substate of the Last Exit from the parent state. A default state can be specified at the beginning.
SignalTransition -- switch status based on specific signals.
TimeoutTransition -- switch status based on the timer.
The following is a comprehensive example:
// Qt5.4 // import the QtQuick module first and then QtQml. stateMachine module import QtQuick 2.4 import QtQuick. controls 1.2 import QtQml. stateMachine 1.0 Item {width: 400 height: 200 Row {anchors. centerIn: parent spacing: 10 Button {id: startButton text: "start" onClicked: {// start the state machine if (! StateMachine. running) {stateMachine. running = true }}button {id: Button // state machine id text: s11.active? "S11": s12.active? "S12": s13.active? "S13": "state"} Button {id: historyButton // history status record text: stateMachine. running? S1.active? "Interrupt": "resume": "history"} Button {id: quitButton text: "quit"} StateMachine {id: stateMachine initialState: s1 // initial status // running: true // The running attribute is set by the button (startButton) State {id: s1 initialState: s11 // initial state SignalTransition {targetState: s3 // switch the State signal: historyButton according to the signal. clicked} SignalTransition {targetState: s2 // switch the state signal: quitButton according to signal. clicked} onEntered: console. log ("s1 entered") onExited: console. log ("s1 exited") State {id: s11 SignalTransition {targetState: s12 // switch the state signal: button according to signal. clicked} onEntered: console. log ("s11 entered") onExited: console. log ("s11 exited")} State {id: s12 SignalTransition {// switch state targetState: s13 signal: button according to signal. clicked} SignalTransition {// signal (quitButton. clicked) signal: quitButton. clicked onTriggered: console. log ("override-quitButton clicked")} onEntered: console. log ("s12 entered") onExited: console. log ("s12 exited")} State {id: s13 SignalTransition {// switch state targetState: s11 signal: button according to signal. clicked} TimeoutTransition {// switch state targetState: s2 timeout: 2000} onEntered: console according to the timer. log ("s13 entered") onExited: console. log ("s13 exited")} HistoryState {// historical status id: history} State {id: s3 SignalTransition {// switch state targetState: history signal: historyButton according to signal. clicked} onEntered: console. log ("s3 entered") onExited: console. log ("s3 exited")} FinalState {id: s2} onFinished: console. log ("state finished ")}}
In addition, the childMode attribute of State is in single-State mode by default. To set an initial State at the beginning, we can also set childMode to parallel mode. When the State enters the parent State, all sub-States are entered at the same time.
For details, refer to the following documents on the Qt Official Website:
Http://doc.qt.io/qt-5/qmlstatemachine.html