QStateMachine 类提供分层有限状态机。 更多...
头: | #include <QStateMachine> |
qmake: | QT += core |
Since: | Qt 4.6 |
继承: | QState |
该类在 Qt 4.6 引入。
注意: 此类的所有函数 可重入 .
注意: 这些函数也是 thread-safe :
class | SignalEvent |
class | WrappedEvent |
enum | Error { NoError, NoInitialStateError, NoDefaultStateInHistoryStateError, NoCommonAncestorForTransitionError, StateMachineChildModeSetToParallelError } |
enum | EventPriority { NormalPriority, HighPriority } |
QStateMachine (QObject * parent = nullptr) | |
virtual | ~QStateMachine () |
void | addDefaultAnimation (QAbstractAnimation * animation ) |
void | addState (QAbstractState * state ) |
bool | cancelDelayedEvent (int id ) |
void | clearError () |
QSet<QAbstractState *> | configuration () const |
QList<QAbstractAnimation *> | defaultAnimations () const |
QStateMachine::Error | error () const |
QString | errorString () const |
QState::RestorePolicy | globalRestorePolicy () const |
bool | isAnimated () const |
bool | isRunning () const |
int | postDelayedEvent (QEvent * event , int delay ) |
int | postDelayedEvent (QEvent * event , std::chrono::milliseconds delay ) |
void | postEvent (QEvent * event , QStateMachine::EventPriority priority = NormalPriority) |
void | removeDefaultAnimation (QAbstractAnimation * animation ) |
void | removeState (QAbstractState * state ) |
void | setAnimated (bool enabled ) |
void | setGlobalRestorePolicy (QState::RestorePolicy restorePolicy ) |
virtual bool | eventFilter (QObject * watched , QEvent * event ) override |
void | setRunning (bool running ) |
void | start () |
void | stop () |
void | runningChanged (bool running ) |
void | started () |
void | stopped () |
virtual bool | event (QEvent * e ) override |
virtual void | onEntry (QEvent * event ) override |
virtual void | onExit (QEvent * event ) override |
QStateMachine 基于的概念和表示法在 Statecharts 。QStateMachine 属于 状态机框架 .
A state machine manages a set of states (classes that inherit from QAbstractState ) and transitions (descendants of QAbstractTransition ) 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 the SCXML (状态图表 XML) algorithm. The framework's 概述 gives several state graphs and the code to build them.
使用 addState () function to add a top-level state to the state machine. States are removed with the removeState () function. Removing states while the machine is running is discouraged.
Before the machine can be started, the initial state must be set. The initial state is the state that the machine enters when started. You can then start () the state machine. The started () 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 through postEvent (). 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 the finished () signal. You can also stop () the state machine explicitly. The stopped () 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 = new QState(); s1->assignProperty(&button, "text", "Click me"); QFinalState *s2 = new QFinalState(); s1->addTransition(&button, &QPushButton::clicked, s2); machine.addState(s1); machine.addState(s2); machine.setInitialState(s1); machine.start();
This code example uses QState , which inherits QAbstractState 。 QState class provides a state that you can use to set properties and invoke methods on QObject s when the state is entered or exited. It also contains convenience functions for adding transitions, e.g., QSignalTransition s as in this example. See the QState class description for further details.
If an error is encountered, the machine will look for an error state , and if one is available, it will enter this state. The types of errors possible are described by the Error enum. 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.
注意: Important: setting the ChildMode of a state machine to parallel ( ParallelStates ) results in an invalid state machine. It can only be set to (or kept as) ExclusiveStates .
另请参阅 QAbstractState , QAbstractTransition , QState ,和 状态机框架 .
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.
常量 | 值 | 描述 |
---|---|---|
QStateMachine::NoError
|
0
|
没有出现错误。 |
QStateMachine::NoInitialStateError
|
1
|
The machine has entered a QState 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 a QHistoryState which does not have a default state set. The context of this error is the QHistoryState 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. |
QStateMachine::StateMachineChildModeSetToParallelError
|
4
|
The machine's childMode property was set to QState::ParallelStates . This is illegal. Only states may be declared as parallel, not the state machine itself. This enum value was added in Qt 5.14. |
另请参阅 setErrorState ().
This enum type specifies the priority of an event posted to the state machine using postEvent ().
Events of high priority are processed before events of normal priority.
常量 | 值 | 描述 |
---|---|---|
QStateMachine::NormalPriority
|
0
|
事件拥有正常优先级。 |
QStateMachine::HighPriority
|
1
|
事件拥有高优先级。 |
此特性保持是否启用动画
此特性的默认值为
true
.
访问函数:
bool | isAnimated () const |
void | setAnimated (bool enabled ) |
另请参阅 QAbstractTransition::addAnimation ().
This property holds the error string of this state machine
访问函数:
QString | errorString () const |
This property holds the restore policy for states of this state machine.
此特性的默认值为 QState::DontRestoreProperties .
访问函数:
QState::RestorePolicy | globalRestorePolicy () const |
void | setGlobalRestorePolicy (QState::RestorePolicy restorePolicy ) |
This property holds the running state of this state machine
该特性在 Qt 5.4 引入。
访问函数:
bool | isRunning () const |
void | setRunning (bool running ) |
通知程序信号:
void | runningChanged (bool running ) |
另请参阅 start (), stop (), started (), stopped (),和 runningChanged ().
Constructs a new state machine with the given parent .
[signal]
void
QStateMachine::
runningChanged
(
bool
running
)
This signal is emitted when the running property is changed with running 作为自变量。
注意: 通知程序信号对于特性 running .
该函数在 Qt 5.4 引入。
另请参阅 QStateMachine::running .
[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 the finished () 信号。
注意: A state machine will not run without a running event loop, such as the main application event loop started with QCoreApplication::exec () 或 QApplication::exec ().
另请参阅 started (), finished (), stop (), initialState (),和 setRunning ().
[signal]
void
QStateMachine::
started
()
This signal is emitted when the state machine has entered its initial state ( QStateMachine::initialState ).
注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。
另请参阅 QStateMachine::finished () 和 QStateMachine::start ().
[slot]
void
QStateMachine::
stop
()
Stops this state machine. The state machine will stop processing events and then emit the stopped () 信号。
另请参阅 stopped (), start (),和 setRunning ().
[signal]
void
QStateMachine::
stopped
()
This signal is emitted when the state machine has stopped.
注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。
另请参阅 QStateMachine::stop () 和 QStateMachine::finished ().
[虚拟]
QStateMachine::
~QStateMachine
()
Destroys this state machine.
Adds a default animation to be considered for any transition.
添加给定 state to this state machine. The state becomes a top-level state and the state machine takes ownership of the 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.
另请参阅 removeState () 和 setInitialState ().
Cancels the delayed event identified by the given
id
. The id should be a value returned by a call to
postDelayedEvent
()。返回
true
if the event was successfully cancelled, otherwise returns
false
.
注意: 此函数是 thread-safe .
另请参阅 postDelayedEvent ().
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 state
s
is in the configuration, it is always the case that the parent of
s
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.
Returns the error string of the last error that occurred in the state machine.
注意: Getter function for property errorString.
[override virtual protected]
bool
QStateMachine::
event
(
QEvent
*
e
)
重实现: QState::event (QEvent *e).
[override virtual]
bool
QStateMachine::
eventFilter
(
QObject
*
watched
,
QEvent
*
event
)
重实现: QObject::eventFilter (QObject *watched, QEvent *event).
Returns the restore policy of the state machine.
注意: Getter function for property globalRestorePolicy.
另请参阅 setGlobalRestorePolicy ().
Returns whether animations are enabled for this state machine.
注意: getter 函数对于特性 animated .
[override virtual protected]
void
QStateMachine::
onEntry
(
QEvent
*
event
)
重实现: QState::onEntry (QEvent *event).
This function will call start () to start the state machine.
[override virtual protected]
void
QStateMachine::
onExit
(
QEvent
*
event
)
重实现: QState::onExit (QEvent *event).
This function will call stop () to stop the state machine and subsequently emit the stopped () 信号。
Posts the given event for processing by this state machine, with the given delay 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.
注意: 此函数是 thread-safe .
另请参阅 cancelDelayedEvent () 和 postEvent ().
这是重载函数。
Posts the given event for processing by this state machine, with the given delay 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.
注意: 此函数是 thread-safe .
该函数在 Qt 5.15 引入。
另请参阅 cancelDelayedEvent () 和 postEvent ().
Posts the given event 为给定 priority 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 or when it is starting up.
注意: 此函数是 thread-safe .
另请参阅 postDelayedEvent ().
移除 animation from the list of default animations.
移除给定 state from this state machine. The state machine releases ownership of the state.
另请参阅 addState ().
Sets whether animations are enabled for this state machine.
注意: setter 函数对于特性 animated .
另请参阅 isAnimated ().
Sets the restore policy of the state machine to restorePolicy . The default restore policy is QState::DontRestoreProperties .
注意: setter 函数对于特性 globalRestorePolicy .
另请参阅 globalRestorePolicy ().