StateMachine QML Type

Provides a hierarchical finite state machine. 更多...

import 语句: import QtQml.StateMachine 1.12
Since: Qt 5.4
继承: State

特性

信号

方法

详细描述

StateMachine is based on the concepts and notation of Statecharts . StateMachine 属于 声明状态机框架 .

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 SCXML (状态图表 XML) algorithm. The framework's 概述 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 2.0
import QtQml.StateMachine 1.0 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.


另请参阅 QAbstractState , State , SignalTransition , TimeoutTransition , HistoryState ,和 声明状态机框架 .

特性文档编制

[read-only] errorString : string

The error string of this state machine.

globalRestorePolicy : enumeration

The restore policy for states of this state machine.

此特性的默认值为 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.

  • QState .DontRestoreProperties The state machine should not save the initial values of properties and restore them later.
  • QState .RestoreProperties The state machine should save the initial values of properties and restore them later.

running : bool

The running state of this state machine.

另请参阅 start() and stop() .


信号文档编制

started ()

This signal is emitted when the state machine has entered its initial state ( State::initialState ).

相应处理程序是 onStarted .

另请参阅 running , start() ,和 State::finished .

stopped ()

This signal is emitted when the state machine has stopped.

相应处理程序是 onStopped .

另请参阅 running , stop() ,和 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.

注意: 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() .

另请参阅 started , State::finished , stop() , State::initialState ,和 running .

stop ()

Stops this state machine. The state machine will stop processing events and then emit the stopped signal.

另请参阅 stopped , start() ,和 running .