PySide6.QtStateMachine.QStateMachine¶
- class QStateMachine¶
The
QStateMachineclass provides a hierarchical finite state machine.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QStateMachineis based on the concepts and notation of Statecharts .QStateMachineis part of Qt 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 theremoveState()function. Removing states while the machine is running is discouraged.Before the machine can be started, the
initial statemust 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.,QEventTransitionand 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.clicked, s2) machine.addState(s1) machine.addState(s2) machine.setInitialState(s1) machine.start()
This code example uses
QState, which inherits QAbstractState . TheQStateclass 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., QSignalTransition s as in this example. See theQStateclass 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 theErrorenum. 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
ChildModeof a state machine to parallel (ParallelStates) results in an invalid state machine. It can only be set to (or kept as)ExclusiveStates.See also
QAbstractStateQAbstractTransitionQStateQt State Machine OverviewSynopsis¶
Properties¶
animatedᅟ- Whether animations are enablederrorStringᅟ- The error string of this state machineglobalRestorePolicyᅟ- The restore policy for states of this state machinerunningᅟ- The running state of this state machine
Methods¶
def
__init__()def
addState()def
clearError()def
configuration()def
error()def
errorString()def
isAnimated()def
isRunning()def
postEvent()def
removeState()def
setAnimated()
Virtual methods¶
def
beginMicrostep()def
endMicrostep()
Slots¶
def
setRunning()def
start()def
stop()
Signals¶
def
runningChanged()def
started()def
stopped()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- class 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.
- class 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
QStatewith 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
QHistoryStatewhich does not have a default state set. The context of this error is theQHistoryStatewhich 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
childModeproperty 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
setErrorState()
Note
Properties can be used directly when
from __feature__ import true_propertyis used or via accessor functions otherwise.- property animatedᅟ: bool¶
This property holds whether animations are enabled.
The default value of this property is
true.See also
addAnimation()- Access functions:
- property errorStringᅟ: str¶
This property holds the error string of this state machine.
- Access functions:
- property globalRestorePolicyᅟ: QState.RestorePolicy¶
This property holds the restore policy for states of this state machine..
The default value of this property is
DontRestoreProperties.- Access functions:
- property runningᅟ: bool¶
This property holds the running state of this state machine.
See also
- Access functions:
Constructs a new state machine with the given
parent.Constructs a new state machine with the given
childModeandparent.Warning
Do not set the
childModeto anything else thanExclusiveStates, otherwise the state machine is invalid, and might work incorrectly.- addDefaultAnimation(animation)¶
- Parameters:
animation –
QAbstractAnimation
Adds a default
animationto be considered for any transition.- addState(state)¶
- Parameters:
state –
QAbstractState
Adds the given
stateto 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
removeState()setInitialState()- 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(). Returnstrueif the event was successfully cancelled, otherwise returnsfalse.See also
- clearError()¶
Clears the error string and error code of the state machine.
- configuration()¶
- Return type:
.QSetQAbstractState
Returns the maximal consistent set of states (including parallel and final states) that this state machine is currently in. If a state
sis in the configuration, it is always the case that the parent ofsis also in c. Note, however, that the machine itself is not an explicit member of the configuration.Returns the error code of the last error that occurred in the state machine.
- errorString()¶
- Return type:
str
Returns the error string of the last error that occurred in the state machine.
Getter of property
errorStringᅟ.- globalRestorePolicy()¶
- Return type:
Returns the restore policy of the state machine.
See also
Getter of property
globalRestorePolicyᅟ.- isAnimated()¶
- Return type:
bool
Returns whether animations are enabled for this state machine.
Getter of property
animatedᅟ.- isRunning()¶
- Return type:
bool
Getter of property
runningᅟ.Posts the given
eventfor processing by this state machine, with the givendelayin 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
- postDelayedEvent(event, delay)
- Parameters:
event –
QEventdelay –
std::chrono::milliseconds
- Return type:
int
Posts the given
eventfor processing by this state machine, with the givendelayin 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
- postEvent(event[, priority=QStateMachine.EventPriority.NormalPriority])¶
- Parameters:
event –
QEventpriority –
EventPriority
Posts the given
eventof the givenpriorityfor 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
- removeDefaultAnimation(animation)¶
- Parameters:
animation –
QAbstractAnimation
Removes
animationfrom the list of default animations.- removeState(state)¶
- Parameters:
state –
QAbstractState
Removes the given
statefrom this state machine. The state machine releases ownership of the state.See also
- runningChanged(running)¶
- Parameters:
running – bool
This signal is emitted when the running property is changed with
runningas argument.See also
runningNotification signal of property
runningᅟ.- setAnimated(enabled)¶
- Parameters:
enabled – bool
Sets whether animations are
enabledfor this state machine.See also
Setter of property
animatedᅟ.- setGlobalRestorePolicy(restorePolicy)¶
- Parameters:
restorePolicy –
RestorePolicy
Sets the restore policy of the state machine to
restorePolicy. The default restore policy isDontRestoreProperties.See also
Setter of property
globalRestorePolicyᅟ.- setRunning(running)¶
- Parameters:
running – bool
See also
Setter of property
runningᅟ.- 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 QCoreApplication::exec() or QApplication::exec().
See also
started()finished()stop()initialState()setRunning()- started()¶
This signal is emitted when the state machine has entered its initial state (QStateMachine::initialState).
See also
finished()start()- stop()¶
Stops this state machine. The state machine will stop processing events and then emit the
stopped()signal.See also
- stopped()¶
This signal is emitted when the state machine has stopped.
See also
stop()finished()- class WrappedEvent¶
The
WrappedEventclass inherits QEvent and holds a clone of an event associated with a QObject.Details
A wrapped event is generated by a
QStateMachinein response to a Qt event. TheQEventTransitionclass provides a transition associated with a such an event.WrappedEventis part of Qt State Machine Overview .The
object()function returns the object that generated the event. Theevent()function returns a clone of the original event.See also
Synopsis¶
Methods¶
def
__init__()def
event()def
object()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Returns a clone of the original event.
Returns the object that the event is associated with.
- class SignalEvent¶
The
SignalEventclass represents a Qt signal event.Details
A signal event is generated by a
QStateMachinein response to a Qt signal. The QSignalTransition class provides a transition associated with a signal event.SignalEventis part of Qt State Machine Framework .The
sender()function returns the object that generated the signal. ThesignalIndex()function returns the index of the signal. Thearguments()function returns the arguments of the signal.See also
Synopsis¶
Methods¶
def
__init__()def
arguments()def
sender()def
signalIndex()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- __init__(sender, signalIndex, arguments)¶
- Parameters:
sender –
QObjectsignalIndex – int
arguments – .list of QVariant
- arguments()¶
- Return type:
.list of QVariant
Returns the arguments of the signal.
Returns the object that emitted the signal.
See also
- signalIndex()¶
- Return type:
int
Returns the index of the signal.
See also
method()