CopperSpice API  1.9.2
QStateMachine Class Reference

QStateMachine class provides a hierarchical finite state machine. More...

Inheritance diagram for QStateMachine:
QState QAbstractState QObject

Classes

class  SignalEvent
 Signal event More...
 
class  WrappedEvent
 Inherits QEvent and holds a clone of an event associated with a QObject More...
 

Public Types

enum  Error
 
enum  EventPriority
 
- Public Types inherited from QState
enum  ChildMode
 
enum  RestorePolicy
 

Public Signals

void runningChanged (bool running)
 
void started ()
 
void stopped ()
 
- Public Signals inherited from QState
void childModeChanged ()
 
void errorStateChanged ()
 
void finished ()
 
void initialStateChanged ()
 
void propertiesAssigned ()
 
- Public Signals inherited from QAbstractState
void activeChanged (bool active)
 
void entered ()
 
void exited ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void setRunning (bool running)
 
void start ()
 
void stop ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QStateMachine (QObject *parent=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
 
bool eventFilter (QObject *watched, QEvent *event) override
 
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)
 
- Public Methods inherited from QState
 QState (ChildMode childMode, QState *parent=nullptr)
 
 QState (QState *parent=nullptr)
 
 ~QState ()
 
QAbstractTransitionaddTransition (QAbstractState *target)
 
void addTransition (QAbstractTransition *transition)
 
template<class SignalClass , class... SignalArgs>
QSignalTransitionaddTransition (QObject *sender, void (SignalClass::*signal)(SignalArgs...), QAbstractState *target)
 
void assignProperty (QObject *object, const QString &name, const QVariant &value)
 
ChildMode childMode () const
 
QAbstractStateerrorState () const
 
QAbstractStateinitialState () const
 
void removeTransition (QAbstractTransition *transition)
 
void setChildMode (ChildMode mode)
 
void setErrorState (QAbstractState *state)
 
void setInitialState (QAbstractState *state)
 
QList< QAbstractTransition * > transitions () const
 
- Public Methods inherited from QAbstractState
 ~QAbstractState ()
 
bool active () const
 
QStateMachinemachine () const
 
QStateparentState () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

bool event (QEvent *event) override
 
void onEntry (QEvent *event) override
 
void onExit (QEvent *event) override
 
- Protected Methods inherited from QAbstractState
 QAbstractState (QState *parent=nullptr)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Properties

 animated
 
 errorString
 
 globalRestorePolicy
 
 running
 
- Properties inherited from QState
 childMode
 
 errorState
 
 initialState
 
- Properties inherited from QAbstractState
 active
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Detailed Description

The QStateMachine class provides a hierarchical finite state machine. This class is based on the concepts and notation of "Statecharts: A visual formalism for complex systems".

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: State Machine Notation for Control Abstraction" algorithm. The State Machine 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 the CopperSpice 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 code shows a state machine that will finish when a button is clicked:

QPushButton button;
QState *s1 = new QState();
s1->assignProperty(&button, "text", "Click me");
s1->addTransition(&button, SIGNAL(clicked()), s2);

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.

See also
QAbstractState, QAbstractTransition, QState, State Machine

Member Enumeration Documentation

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.

ConstantValueDescription
QStateMachine::NoError0No error has occurred.
QStateMachine::NoInitialStateError1 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::NoDefaultStateInHistoryStateError2 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::NoCommonAncestorForTransitionError3 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.
See also
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.

ConstantValueDescription
QStateMachine::NormalPriority0The event has normal priority.
QStateMachine::HighPriority1The event has high priority.

Constructor & Destructor Documentation

QStateMachine::QStateMachine ( QObject parent = nullptr)
explicit

Constructs a new state machine with the given parent.

QStateMachine::~QStateMachine ( )

Destroys this state machine.

Method Documentation

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.

See also
removeState(), 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 canceled, otherwise returns false.

Note
This method is thread-safe.
See also
postDelayedEvent()
void QStateMachine::clearError ( )

Clears the error string and error code of the state machine.

QSet< QAbstractState * > QStateMachine::configuration ( ) const

Returns the complete set of states that this state machine is currently in. If a state s is in this set then its parent is also in the set.

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 value of the property errorString which contains the last error that occurred in the state machine.

bool QStateMachine::event ( QEvent event)
overrideprotectedvirtual

Reimplemented from QState::event()

bool QStateMachine::eventFilter ( QObject watched,
QEvent event 
)
overridevirtual

Reimplemented from QObject::eventFilter()

QState::RestorePolicy QStateMachine::globalRestorePolicy ( ) const

Returns the restore policy of the state machine.

See also
setGlobalRestorePolicy()
bool QStateMachine::isAnimated ( ) const

Returns the value of the property animations which indicates whether animations are enabled for this state machine.

bool QStateMachine::isRunning ( ) const

Returns whether this state machine is running.

See also
start(), stop()
void QStateMachine::onEntry ( QEvent event)
overrideprotectedvirtual

This method will call start() to start the state machine.

Reimplemented from QState::onEntry()

void QStateMachine::onExit ( QEvent event)
overrideprotectedvirtual

This method will call stop() to stop the state machine and subsequently emit the stopped() signal.

Reimplemented from QState::onExit()

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 method 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 method is thread-safe.
See also
cancelDelayedEvent(), postEvent()
void QStateMachine::postEvent ( QEvent event,
EventPriority  priority = NormalPriority 
)

Posts the given event of the given priority for processing by this state machine.

This method 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.

Note
This method is thread-safe.
See also
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.

See also
addState()
void QStateMachine::runningChanged ( bool  running)
signal

This signal is emitted when the running property is changed with running as argument.

See also
QStateMachine::running
void QStateMachine::setAnimated ( bool  enabled)

Sets the value of the property animations to the given enabled. Indicates whether animations are enabled for this state machine.

void QStateMachine::setGlobalRestorePolicy ( QState::RestorePolicy  restorePolicy)

Sets the restore policy of the state machine to restorePolicy. The default restore policy is QState::DontRestoreProperties.

See also
globalRestorePolicy()
void QStateMachine::setRunning ( bool  running)
slot

Sets the running property to the value of running.

See also
QStateMachine::running
void QStateMachine::start ( )
slot

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().
See also
started(), finished(), stop(), initialState()
void QStateMachine::started ( )
signal

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

See also
QStateMachine::finished(), QStateMachine::start()
void QStateMachine::stop ( )
slot

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

See also
stopped(), start()
void QStateMachine::stopped ( )
signal

This signal is emitted when the state machine has stopped.

See also
QStateMachine::stop(), QStateMachine::finished()

Property Documentation

QStateMachine::animated

This property holds whether animations are enabled. The default value of this property is true.

See also
QAbstractTransition::addAnimation()
PropertiesClass Methods
read isAnimated
write setAnimated
QStateMachine::errorString

This property holds the error string of this state machine.

PropertiesClass Methods
read errorString
QStateMachine::globalRestorePolicy

This property holds the restore policy for states of this state machine. The default value of this property is QState::DontRestoreProperties.

PropertiesClass Methods
read globalRestorePolicy
write setGlobalRestorePolicy
QStateMachine::running

This property holds the running state of this state machine.

PropertiesClass Methods
read isRunning
write setRunning
notify runningChanged