QStateMachine¶
The
QStateMachine
class provides a hierarchical finite state machine. More…
New in version 4.6.
Synopsis¶
Functions¶
def
addDefaultAnimation
(animation)def
addState
(state)def
cancelDelayedEvent
(id)def
clearError
()def
configuration
()def
error
()def
errorString
()def
globalRestorePolicy
()def
isAnimated
()def
isRunning
()def
postDelayedEvent
(event, delay)def
postEvent
(event[, priority=NormalPriority])def
removeDefaultAnimation
(animation)def
removeState
(state)def
setAnimated
(enabled)def
setGlobalRestorePolicy
(restorePolicy)
Virtual functions¶
def
beginMicrostep
(event)def
beginSelectTransitions
(event)def
endMicrostep
(event)def
endSelectTransitions
(event)
Slots¶
def
setRunning
(running)def
start
()def
stop
()
Signals¶
def
runningChanged
(running)
Detailed Description¶
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 ofQAbstractTransition
) 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 theremoveState()
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 thenstart()
the state machine. Thestarted()
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 alsostop()
the state machine explicitly. Thestopped()
signal is emitted in this case.The following snippet shows a state machine that will finish when a button is clicked:
button = QPushButton() machine = QStateMachine() s1 = QState() s1.assignProperty(button, "text", "Click me") s2 = QFinalState() s1.addTransition(button, SIGNAL('clicked()'), s2) machine.addState(s1) machine.addState(s2) machine.setInitialState(s1) machine.start()This code example uses
QState
, which inheritsQAbstractState
. TheQState
class provides a state that you can use to set properties and invoke methods onQObject
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 theQState
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 theError
enum. After the error state is entered, the type of the error can be retrieved witherror()
. 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.Note
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
.
- class PySide2.QtCore.QStateMachine([parent=None])¶
PySide2.QtCore.QStateMachine(childMode[, parent=None])
- param parent:
- param childMode:
Constructs a new state machine with the given
parent
.Constructs a new state machine with the given
childMode
andparent
.Warning
Do not set the
childMode
to anything else thanExclusiveStates
, otherwise the state machine is invalid, and might work incorrectly.
- PySide2.QtCore.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
Description
QStateMachine.NormalPriority
The event has normal priority.
QStateMachine.HighPriority
The event has high priority.
- PySide2.QtCore.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 byerrorString()
, and enter an error state based on the context of the error.Constant
Description
QStateMachine.NoError
No error has occurred.
QStateMachine.NoInitialStateError
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
The machine has entered a
QHistoryState
which does not have a default state set. The context of this error is theQHistoryState
which is missing a default state.QStateMachine.NoCommonAncestorForTransitionError
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
The machine’s
childMode
property was set toParallelStates
. This is illegal. Only states may be declared as parallel, not the state machine itself. This enum value was added in Qt 5.14.See also
- PySide2.QtCore.QStateMachine.addDefaultAnimation(animation)¶
- Parameters:
animation –
PySide2.QtCore.QAbstractAnimation
Adds a default
animation
to be considered for any transition.
- PySide2.QtCore.QStateMachine.addState(state)¶
- Parameters:
state –
PySide2.QtCore.QAbstractState
Adds the given
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.
See also
- PySide2.QtCore.QStateMachine.beginMicrostep(event)¶
- Parameters:
event –
PySide2.QtCore.QEvent
This function is called when the state machine is about to do a microstep.
The default implementation does nothing.
- PySide2.QtCore.QStateMachine.beginSelectTransitions(event)¶
- Parameters:
event –
PySide2.QtCore.QEvent
This function is called when the state machine is about to select transitions based on the given
event
.The default implementation does nothing.
- PySide2.QtCore.QStateMachine.cancelDelayedEvent(id)¶
- Parameters:
id – int
- Return type:
bool
Cancels the delayed event identified by the given
id
. The id should be a value returned by a call topostDelayedEvent()
. Returnstrue
if the event was successfully cancelled, otherwise returnsfalse
.See also
- PySide2.QtCore.QStateMachine.clearError()¶
Clears the error string and error code of the state machine.
- PySide2.QtCore.QStateMachine.configuration()¶
- Return type:
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 ofs
is also in c. Note, however, that the machine itself is not an explicit member of the configuration.
- PySide2.QtCore.QStateMachine.endMicrostep(event)¶
- Parameters:
event –
PySide2.QtCore.QEvent
This function is called when the state machine has finished doing a microstep.
The default implementation does nothing.
- PySide2.QtCore.QStateMachine.endSelectTransitions(event)¶
- Parameters:
event –
PySide2.QtCore.QEvent
This function is called when the state machine has finished selecting transitions based on the given
event
.The default implementation does nothing.
- PySide2.QtCore.QStateMachine.error()¶
- Return type:
Returns the error code of the last error that occurred in the state machine.
- PySide2.QtCore.QStateMachine.errorString()¶
- Return type:
str
This property holds the error string of this state machine.
- PySide2.QtCore.QStateMachine.globalRestorePolicy()¶
- Return type:
This property holds the restore policy for states of this state machine..
The default value of this property is
DontRestoreProperties
.
- PySide2.QtCore.QStateMachine.isAnimated()¶
- Return type:
bool
This property holds whether animations are enabled.
The default value of this property is
true
.See also
- PySide2.QtCore.QStateMachine.isRunning()¶
- Return type:
bool
This property holds the running state of this state machine.
See also
start()
stop()
started()
stopped()
runningChanged()
- PySide2.QtCore.QStateMachine.postDelayedEvent(event, delay)¶
- Parameters:
event –
PySide2.QtCore.QEvent
delay – int
- Return type:
int
Posts the given
event
for processing by this state machine, with the givendelay
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.
See also
- PySide2.QtCore.QStateMachine.postEvent(event[, priority=NormalPriority])¶
- Parameters:
event –
PySide2.QtCore.QEvent
priority –
EventPriority
Posts the given
event
of the givenpriority
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.
See also
- PySide2.QtCore.QStateMachine.removeDefaultAnimation(animation)¶
- Parameters:
animation –
PySide2.QtCore.QAbstractAnimation
Removes
animation
from the list of default animations.
- PySide2.QtCore.QStateMachine.removeState(state)¶
- Parameters:
state –
PySide2.QtCore.QAbstractState
Removes the given
state
from this state machine. The state machine releases ownership of the state.See also
- PySide2.QtCore.QStateMachine.runningChanged(running)¶
- Parameters:
running – bool
- PySide2.QtCore.QStateMachine.setAnimated(enabled)¶
- Parameters:
enabled – bool
This property holds whether animations are enabled.
The default value of this property is
true
.See also
- PySide2.QtCore.QStateMachine.setGlobalRestorePolicy(restorePolicy)¶
- Parameters:
restorePolicy –
RestorePolicy
This property holds the restore policy for states of this state machine..
The default value of this property is
DontRestoreProperties
.
- PySide2.QtCore.QStateMachine.setRunning(running)¶
- Parameters:
running – bool
This property holds the running state of this state machine.
See also
start()
stop()
started()
stopped()
runningChanged()
- PySide2.QtCore.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 thefinished()
signal.Note
A state machine will not run without a running event loop, such as the main application event loop started with
exec()
orexec()
.See also
started()
finished()
stop()
initialState()
setRunning()
- PySide2.QtCore.QStateMachine.stop()¶
Stops this state machine. The state machine will stop processing events and then emit the
stopped()
signal.See also
stopped()
start()
setRunning()
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.