QStateMachine Class
The QStateMachine class provides a hierarchical finite state machine. 更多...
头文件: | #include <QStateMachine> |
qmake: | QT += core |
开始支持版本: | Qt 4.6 |
基类: | QState |
Note: All functions in this class are reentrant.
Note: These functions are also thread-safe:
- postEvent(QEvent *event, EventPriority priority)
- postDelayedEvent(QEvent *event, int delay)
- cancelDelayedEvent(int id)
公有类型
class | SignalEvent |
class | WrappedEvent |
enum | Error { NoError, NoInitialStateError, NoDefaultStateInHistoryStateError, NoCommonAncestorForTransitionError } |
enum | EventPriority { NormalPriority, HighPriority } |
属性
- animated : bool
- errorString : const QString
- globalRestorePolicy : QState::RestorePolicy
- running : bool
- 3 个属性继承自 QState
- 1 个属性继承自 QAbstractState
- 1 个属性继承自 QObject
公有函数
QStateMachine(QObject *parent = Q_NULLPTR) | |
QStateMachine(QState::ChildMode childMode, QObject *parent = Q_NULLPTR) | |
~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 |
QState::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(QState::RestorePolicy restorePolicy) |
重新实现的公有函数
virtual bool | eventFilter(QObject *watched, QEvent *event) |
- 13 个公有函数继承自 QState
- 3 个公有函数继承自 QAbstractState
- 32 个公有函数继承自 QObject
公有槽函数
void | setRunning(bool running) |
void | start() |
void | stop() |
- 1 个公有槽函数继承自 QObject
信号
void | runningChanged(bool running) |
void | started() |
void | stopped() |
- 5 个信号继承自 QState
- 3 个信号继承自 QAbstractState
- 2 个信号继承自 QObject
重新实现的受保护函数
virtual bool | event(QEvent *e) |
virtual void | onEntry(QEvent *event) |
virtual void | onExit(QEvent *event) |
- 3 个受保护的函数继承自 QState
- 3 个受保护的函数继承自 QAbstractState
- 9 个受保护的函数继承自 QObject
其他继承的成员
- 11 个静态公有成员继承自 QObject
- 3 个受保护的函数继承自 QState
- 3 个受保护的函数继承自 QAbstractState
- 9 个受保护的函数继承自 QObject
详细描述
The QStateMachine class provides a hierarchical finite state machine.
QStateMachine is based on the concepts and notation of Statecharts. QStateMachine is part of The State Machine Framework.
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 State Chart XML (SCXML) algorithm. The framework's overview gives several state graphs and the code to build them.
Use the 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, SIGNAL(clicked()), s2); machine.addState(s1); machine.addState(s2); machine.setInitialState(s1); machine.start();
This code example uses QState, which inherits QAbstractState. The QState class provides a state that you can use to set properties and invoke methods on QObjects when the state is entered or exited. It also contains convenience functions for adding transitions, e.g., QSignalTransitions 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.
参见 QAbstractState, QAbstractTransition, QState, and The State Machine Framework.
成员类型
enum QStateMachine::Error
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.
Constant | Value | Description |
---|---|---|
QStateMachine::NoError | 0 | No error has occurred. |
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. |
参见 setErrorState().
enum QStateMachine::EventPriority
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.
Constant | Value | Description |
---|---|---|
QStateMachine::NormalPriority | 0 | The event has normal priority. |
QStateMachine::HighPriority | 1 | The event has high priority. |
属性
animated : bool
This property holds whether animations are enabled
The default value of this property is true
.
访问函数:
bool | isAnimated() const |
void | setAnimated(bool enabled) |
参见 QAbstractTransition::addAnimation().
errorString : const QString
This property holds the error string of this state machine
访问函数:
QString | errorString() const |
globalRestorePolicy : QState::RestorePolicy
This property holds the restore policy for states of this state machine.
The default value of this property is QState::DontRestoreProperties.
访问函数:
QState::RestorePolicy | globalRestorePolicy() const |
void | setGlobalRestorePolicy(QState::RestorePolicy restorePolicy) |
running : bool
This property holds the running state of this state machine
This property was introduced in Qt 5.4.
访问函数:
bool | isRunning() const |
void | setRunning(bool running) |
Notifier signal:
void | runningChanged(bool running) |
参见 start(), stop(), started(), stopped(), and runningChanged().
成员函数
QStateMachine::QStateMachine(QObject *parent = Q_NULLPTR)
Constructs a new state machine with the given parent.
QStateMachine::QStateMachine(QState::ChildMode childMode, QObject *parent = Q_NULLPTR)
Constructs a new state machine with the given childMode and parent.
This function was introduced in Qt 5.0.
QStateMachine::~QStateMachine()
Destroys this state machine.
void QStateMachine::addDefaultAnimation(QAbstractAnimation *animation)
Adds a default animation to be considered for any transition.
void QStateMachine::addState(QAbstractState *state)
Adds the given state 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.
参见 removeState() and setInitialState().
bool QStateMachine::cancelDelayedEvent(int id)
Cancels the delayed event identified by the given id. The id should be a value returned by a call to postDelayedEvent(). Returns true
if the event was successfully cancelled, otherwise returns false
.
Note: This function is thread-safe
参见 postDelayedEvent().
void QStateMachine::clearError()
Clears the error string and error code of the state machine.
QSet<QAbstractState *> QStateMachine::configuration() const
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.
QList<QAbstractAnimation *> QStateMachine::defaultAnimations() const
Returns the list of default animations that will be considered for any transition.
Error QStateMachine::error() const
Returns the error code of the last error that occurred in the state machine.
QString QStateMachine::errorString() const
Returns the error string of the last error that occurred in the state machine.
Note: Getter function for property errorString.
[virtual protected]
bool QStateMachine::event(QEvent *e)
Reimplemented from QObject::event().
[virtual]
bool QStateMachine::eventFilter(QObject *watched, QEvent *event)
Reimplemented from QObject::eventFilter().
QState::RestorePolicy QStateMachine::globalRestorePolicy() const
Returns the restore policy of the state machine.
Note: Getter function for property globalRestorePolicy.
参见 setGlobalRestorePolicy().
bool QStateMachine::isAnimated() const
Returns whether animations are enabled for this state machine.
Note: Getter function for property animated.
[virtual protected]
void QStateMachine::onEntry(QEvent *event)
Reimplemented from QAbstractState::onEntry().
This function will call start() to start the state machine.
[virtual protected]
void QStateMachine::onExit(QEvent *event)
Reimplemented from QAbstractState::onExit().
This function will call stop() to stop the state machine and subsequently emit the stopped() signal.
int QStateMachine::postDelayedEvent(QEvent *event, int delay)
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.
Note: This function is thread-safe
参见 cancelDelayedEvent() and postEvent().
void QStateMachine::postEvent(QEvent *event, EventPriority priority = NormalPriority)
Posts the given event of the given 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.
Note: This function is thread-safe
参见 postDelayedEvent().
void QStateMachine::removeDefaultAnimation(QAbstractAnimation *animation)
Removes animation from the list of default animations.
void QStateMachine::removeState(QAbstractState *state)
Removes the given state from this state machine. The state machine releases ownership of the state.
参见 addState().
[signal]
void QStateMachine::runningChanged(bool running)
This signal is emitted when the running property is changed with running as argument.
This function was introduced in Qt 5.4.
Note: Notifier signal for property running.
void QStateMachine::setAnimated(bool enabled)
Sets whether animations are enabled for this state machine.
Note: Setter function for property animated.
参见 isAnimated().
void QStateMachine::setGlobalRestorePolicy(QState::RestorePolicy restorePolicy)
Sets the restore policy of the state machine to restorePolicy. The default restore policy is QState::DontRestoreProperties.
Note: Setter function for property globalRestorePolicy.
参见 globalRestorePolicy().
[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() 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().
参见 started(), finished(), stop(), initialState(), and setRunning().
[signal]
void QStateMachine::started()
This signal is emitted when the state machine has entered its initial state (QStateMachine::initialState).
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
参见 QStateMachine::finished() and QStateMachine::start().
[slot]
void QStateMachine::stop()
Stops this state machine. The state machine will stop processing events and then emit the stopped() signal.
参见 stopped(), start(), and setRunning().
[signal]
void QStateMachine::stopped()
This signal is emitted when the state machine has stopped.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
参见 QStateMachine::stop() and QStateMachine::finished().