Qt 4.8
|
The QStateMachine class provides a hierarchical finite state machine. More...
#include <qstatemachine.h>
Classes | |
class | SignalEvent |
The SignalEvent class represents a Qt signal event. More... | |
class | WrappedEvent |
The WrappedEvent class inherits QEvent and holds a clone of an event associated with a QObject. More... | |
Public Types | |
enum | Error { NoError, NoInitialStateError, NoDefaultStateInHistoryStateError, NoCommonAncestorForTransitionError } |
This enum type defines errors that can occur in the state machine at run time. More... | |
enum | EventPriority { NormalPriority, HighPriority } |
This enum type specifies the priority of an event posted to the state machine using postEvent(). More... | |
enum | RestorePolicy { DontRestoreProperties, RestoreProperties } |
This enum specifies the restore policy type. More... | |
Public Types inherited from QState | |
enum | ChildMode { ExclusiveStates, ParallelStates } |
This enum specifies how a state's child states are treated. More... | |
Public Slots | |
void | start () |
Starts this state machine. More... | |
void | stop () |
Stops this state machine. More... | |
Public Slots inherited from QObject | |
void | deleteLater () |
Schedules this object for deletion. More... | |
Signals | |
void | started () |
This signal is emitted when the state machine has entered its initial state (QStateMachine::initialState). More... | |
void | stopped () |
This signal is emitted when the state machine has stopped. More... | |
Signals inherited from QState | |
void | finished () |
This signal is emitted when a final child state of this state is entered. More... | |
void | propertiesAssigned () |
This signal is emitted when all properties have been assigned their final value. More... | |
Signals inherited from QAbstractState | |
void | entered () |
This signal is emitted when the state has been entered (after onEntry() has been called). More... | |
void | exited () |
This signal is emitted when the state has been exited (after onExit() has been called). More... | |
Signals inherited from QObject | |
void | destroyed (QObject *=0) |
This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More... | |
Public Functions | |
void | addDefaultAnimation (QAbstractAnimation *animation) |
Adds a default animation to be considered for any transition. More... | |
void | addState (QAbstractState *state) |
Adds the given state to this state machine. More... | |
bool | cancelDelayedEvent (int id) |
Cancels the delayed event identified by the given id. More... | |
void | clearError () |
Clears the error string and error code of the state machine. More... | |
QSet< QAbstractState * > | configuration () const |
Returns the maximal consistent set of states (including parallel and final states) that this state machine is currently in. More... | |
QList< QAbstractAnimation * > | defaultAnimations () const |
Returns the list of default animations that will be considered for any transition. More... | |
Error | error () const |
Returns the error code of the last error that occurred in the state machine. More... | |
QString | errorString () const |
bool | eventFilter (QObject *watched, QEvent *event) |
Reimplemented Function More... | |
QStateMachine::RestorePolicy | globalRestorePolicy () const |
bool | isAnimated () const |
Returns whether animations are enabled for this state machine. More... | |
bool | isRunning () const |
Returns whether this state machine is running. More... | |
int | postDelayedEvent (QEvent *event, int delay) |
Posts the given event for processing by this state machine, with the given delay in milliseconds. More... | |
void | postEvent (QEvent *event, EventPriority priority=NormalPriority) |
Posts the given event of the given priority for processing by this state machine. More... | |
QStateMachine (QObject *parent=0) | |
Constructs a new state machine with the given parent. More... | |
void | removeDefaultAnimation (QAbstractAnimation *animation) |
Removes animation from the list of default animations. More... | |
void | removeState (QAbstractState *state) |
Removes the given state from this state machine. More... | |
void | setAnimated (bool enabled) |
Sets whether animations are enabled for this state machine. More... | |
void | setGlobalRestorePolicy (QStateMachine::RestorePolicy restorePolicy) |
Sets the restore policy of the state machine to restorePolicy. More... | |
~QStateMachine () | |
Destroys this state machine. More... | |
Public Functions inherited from QState | |
void | addTransition (QAbstractTransition *transition) |
Adds the given transition. More... | |
QSignalTransition * | addTransition (QObject *sender, const char *signal, QAbstractState *target) |
Adds a transition associated with the given signal of the given sender object, and returns the new QSignalTransition object. More... | |
QAbstractTransition * | addTransition (QAbstractState *target) |
Adds an unconditional transition from this state to the given target state, and returns then new transition object. More... | |
void | assignProperty (QObject *object, const char *name, const QVariant &value) |
Instructs this state to set the property with the given name of the given object to the given value when the state is entered. More... | |
ChildMode | childMode () const |
QAbstractState * | errorState () const |
QAbstractState * | initialState () const |
QState (QState *parent=0) | |
Constructs a new state with the given parent state. More... | |
QState (ChildMode childMode, QState *parent=0) | |
Constructs a new state with the given childMode and the given parent state. More... | |
void | removeTransition (QAbstractTransition *transition) |
Removes the given transition from this state. More... | |
void | setChildMode (ChildMode mode) |
Sets the child mode of this state. More... | |
void | setErrorState (QAbstractState *state) |
Sets this state's error state to be the given state. More... | |
void | setInitialState (QAbstractState *state) |
Sets this state's initial state to be the given state. More... | |
QList< QAbstractTransition * > | transitions () const |
Returns this state's outgoing transitions (i. More... | |
~QState () | |
Destroys this state. More... | |
Public Functions inherited from QAbstractState | |
QStateMachine * | machine () const |
Returns the state machine that this state is part of, or 0 if the state is not part of a state machine. More... | |
QState * | parentState () const |
Returns this state's parent state, or 0 if the state has no parent state. More... | |
~QAbstractState () | |
Destroys this state. More... | |
Public Functions inherited from QObject | |
bool | blockSignals (bool b) |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More... | |
const QObjectList & | children () const |
Returns a list of child objects. More... | |
bool | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0) |
bool | disconnect (const QObject *receiver, const char *member=0) |
void | dumpObjectInfo () |
Dumps information about signal connections, etc. More... | |
void | dumpObjectTree () |
Dumps a tree of children to the debug output. More... | |
QList< QByteArray > | dynamicPropertyNames () const |
Returns the names of all properties that were dynamically added to the object using setProperty(). More... | |
template<typename T > | |
T | findChild (const QString &aName=QString()) const |
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More... | |
template<typename T > | |
QList< T > | findChildren (const QString &aName=QString()) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More... | |
template<typename T > | |
QList< T > | findChildren (const QRegExp &re) const |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More... | |
void | installEventFilter (QObject *) |
Installs an event filter filterObj on this object. More... | |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false. More... | |
void | killTimer (int id) |
Kills the timer with timer identifier, id. More... | |
virtual const QMetaObject * | metaObject () const |
Returns a pointer to the meta-object of this object. More... | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. More... | |
QString | objectName () const |
QObject * | parent () const |
Returns a pointer to the parent object. More... | |
QVariant | property (const char *name) const |
Returns the value of the object's name property. More... | |
Q_INVOKABLE | QObject (QObject *parent=0) |
Constructs an object with parent object parent. More... | |
void | removeEventFilter (QObject *) |
Removes an event filter object obj from this object. More... | |
void | setObjectName (const QString &name) |
void | setParent (QObject *) |
Makes the object a child of parent. More... | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. More... | |
void | setUserData (uint id, QObjectUserData *data) |
bool | signalsBlocked () const |
Returns true if signals are blocked; otherwise returns false. More... | |
int | startTimer (int interval) |
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (uint id) const |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. More... | |
Protected Functions | |
virtual void | beginMicrostep (QEvent *event) |
This function is called when the state machine is about to do a microstep. More... | |
virtual void | beginSelectTransitions (QEvent *event) |
This function is called when the state machine is about to select transitions based on the given event. More... | |
virtual void | endMicrostep (QEvent *event) |
This function is called when the state machine has finished doing a microstep. More... | |
virtual void | endSelectTransitions (QEvent *event) |
This function is called when the state machine has finished selecting transitions based on the given event. More... | |
bool | event (QEvent *e) |
Reimplemented Function More... | |
void | onEntry (QEvent *event) |
This function will call start() to start the state machine. More... | |
void | onExit (QEvent *event) |
This function will call stop() to stop the state machine and subsequently emit the stopped() signal. More... | |
QStateMachine (QStateMachinePrivate &dd, QObject *parent) | |
Protected Functions inherited from QState | |
QState (QStatePrivate &dd, QState *parent) | |
Protected Functions inherited from QAbstractState | |
QAbstractState (QState *parent=0) | |
Constructs a new state with the given parent state. More... | |
QAbstractState (QAbstractStatePrivate &dd, QState *parent) | |
Protected Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *) |
This event handler can be reimplemented in a subclass to receive child events. More... | |
virtual void | connectNotify (const char *signal) |
This virtual function is called when something has been connected to signal in this object. More... | |
virtual void | customEvent (QEvent *) |
This event handler can be reimplemented in a subclass to receive custom events. More... | |
virtual void | disconnectNotify (const char *signal) |
This virtual function is called when something has been disconnected from signal in this object. More... | |
QObject (QObjectPrivate &dd, QObject *parent=0) | |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. More... | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More... | |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *) |
This event handler can be reimplemented in a subclass to receive timer events for the object. More... | |
Properties | |
bool | animated |
whether animations are enabled More... | |
QString | errorString |
the error string of this state machine More... | |
RestorePolicy | globalRestorePolicy |
the restore policy for states of this state machine. More... | |
Additional Inherited Members | |
Static Public Functions inherited from QObject | |
static bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More... | |
static bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
Disconnects signal in object sender from method in object receiver. More... | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static uint | registerUserData () |
static QString | tr (const char *sourceText, const char *comment=0, int n=-1) |
static QString | trUtf8 (const char *sourceText, const char *comment=0, int n=-1) |
Static Public Variables inherited from QObject | |
static const QMetaObject | staticMetaObject |
This variable stores the meta-object for the class. More... | |
Protected Variables inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
The QStateMachine class provides a hierarchical finite state machine.
QStateMachine is based on the concepts and notation of Statecharts: A visual formalism for complex systems{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: State Machine Notation for Control Abstraction{State Chart XML (SCXML)} algorithm. The framework's The State Machine Framework{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:
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 QObject when the state is entered or exited. It also contains convenience functions for adding transitions, e.g., QSignalTransition 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 QStateMachine::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.
Definition at line 63 of file qstatemachine.h.
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.
Enumerator | |
---|---|
NoError | |
NoInitialStateError | |
NoDefaultStateInHistoryStateError | |
NoCommonAncestorForTransitionError |
Definition at line 116 of file qstatemachine.h.
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.
Enumerator | |
---|---|
NormalPriority | |
HighPriority |
Definition at line 106 of file qstatemachine.h.
This enum specifies the restore policy type.
The restore policy takes effect when the machine enters a state which sets one or more properties. If the restore policy is set to RestoreProperties, the state machine will save the original value of the property before the new value is set.
Later, when the machine either enters a state which does not set a value for the given property, the property will automatically be restored to its initial value.
Only one initial value will be saved for any given property. If a value for a property has already been saved by the state machine, it will not be overwritten until the property has been successfully restored.
Enumerator | |
---|---|
DontRestoreProperties | |
RestoreProperties |
Definition at line 111 of file qstatemachine.h.
QStateMachine::QStateMachine | ( | QObject * | parent = 0 | ) |
Constructs a new state machine with the given parent.
Definition at line 1685 of file qstatemachine.cpp.
QStateMachine::~QStateMachine | ( | ) |
|
protected |
Definition at line 1696 of file qstatemachine.cpp.
void QStateMachine::addDefaultAnimation | ( | QAbstractAnimation * | animation | ) |
Adds a default animation to be considered for any transition.
Definition at line 2244 of file qstatemachine.cpp.
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.
Definition at line 1839 of file qstatemachine.cpp.
|
protectedvirtual |
This function is called when the state machine is about to do a microstep.
The default implementation does nothing.
Definition at line 2173 of file qstatemachine.cpp.
|
protectedvirtual |
This function is called when the state machine is about to select transitions based on the given event.
The default implementation does nothing.
Definition at line 2142 of file qstatemachine.cpp.
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.
Definition at line 2037 of file qstatemachine.cpp.
void QStateMachine::clearError | ( | ) |
Clears the error string and error code of the state machine.
Definition at line 1800 of file qstatemachine.cpp.
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.
Definition at line 2060 of file qstatemachine.cpp.
QList< QAbstractAnimation * > QStateMachine::defaultAnimations | ( | ) | const |
Returns the list of default animations that will be considered for any transition.
Definition at line 2253 of file qstatemachine.cpp.
|
protectedvirtual |
This function is called when the state machine has finished doing a microstep.
The default implementation does nothing.
Definition at line 2189 of file qstatemachine.cpp.
|
protectedvirtual |
This function is called when the state machine has finished selecting transitions based on the given event.
The default implementation does nothing.
Definition at line 2158 of file qstatemachine.cpp.
QStateMachine::Error QStateMachine::error | ( | ) | const |
Returns the error code of the last error that occurred in the state machine.
Definition at line 1782 of file qstatemachine.cpp.
|
protectedvirtual |
Reimplemented Function
Reimplemented from QState.
Definition at line 2092 of file qstatemachine.cpp.
Referenced by postDelayedEvent(), and postEvent().
Reimplemented Function
Reimplemented from QObject.
Definition at line 2123 of file qstatemachine.cpp.
QStateMachine::RestorePolicy QStateMachine::globalRestorePolicy | ( | ) | const |
Referenced by clearError().
bool QStateMachine::isAnimated | ( | ) | const |
Returns whether animations are enabled for this state machine.
Definition at line 2226 of file qstatemachine.cpp.
bool QStateMachine::isRunning | ( | ) | const |
Returns whether this state machine is running.
Definition at line 1878 of file qstatemachine.cpp.
|
protectedvirtual |
This function will call start() to start the state machine.
Reimplemented Function
Reimplemented from QState.
Definition at line 2201 of file qstatemachine.cpp.
|
protectedvirtual |
This function will call stop() to stop the state machine and subsequently emit the stopped() signal.
Reimplemented Function
Reimplemented from QState.
Definition at line 2215 of file qstatemachine.cpp.
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.
Definition at line 2001 of file qstatemachine.cpp.
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.
Definition at line 1957 of file qstatemachine.cpp.
void QStateMachine::removeDefaultAnimation | ( | QAbstractAnimation * | animation | ) |
Removes animation from the list of default animations.
Definition at line 2262 of file qstatemachine.cpp.
void QStateMachine::removeState | ( | QAbstractState * | state | ) |
Removes the given state from this state machine.
The state machine releases ownership of the state.
Definition at line 1858 of file qstatemachine.cpp.
void QStateMachine::setAnimated | ( | bool | enabled | ) |
Sets whether animations are enabled for this state machine.
Definition at line 2235 of file qstatemachine.cpp.
void QStateMachine::setGlobalRestorePolicy | ( | QStateMachine::RestorePolicy | restorePolicy | ) |
Sets the restore policy of the state machine to restorePolicy.
The default restore policy is QAbstractState::DontRestoreProperties.
Definition at line 1824 of file qstatemachine.cpp.
|
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.
Definition at line 1895 of file qstatemachine.cpp.
Referenced by onEntry().
|
signal |
This signal is emitted when the state machine has entered its initial state (QStateMachine::initialState).
|
slot |
Stops this state machine.
The state machine will stop processing events and then emit the stopped() signal.
Definition at line 1923 of file qstatemachine.cpp.
Referenced by onExit().
|
signal |
This signal is emitted when the state machine has stopped.
|
private |
whether animations are enabled
The default value of this property is true.
Definition at line 70 of file qstatemachine.h.
|
private |
the error string of this state machine
Returns the error string of the last error that occurred in the state machine.
Definition at line 66 of file qstatemachine.h.
|
private |
the restore policy for states of this state machine.
Returns the restore policy of the state machine.
The default value of this property is QStateMachine::DontRestoreProperties.
Definition at line 67 of file qstatemachine.h.