Qt 4.8
Classes | Public Types | Public Slots | Signals | Public Functions | Protected Functions | Properties | List of all members
QStateMachine Class Reference

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

#include <qstatemachine.h>

Inheritance diagram for QStateMachine:
QState QAbstractState QObject

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...
 
QSignalTransitionaddTransition (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...
 
QAbstractTransitionaddTransition (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
 
QAbstractStateerrorState () const
 
QAbstractStateinitialState () 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
QStateMachinemachine () const
 Returns the state machine that this state is part of, or 0 if the state is not part of a state machine. More...
 
QStateparentState () 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 QObjectListchildren () 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< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
template<typename 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 QMetaObjectmetaObject () 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
 
QObjectparent () 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...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (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...
 
QObjectsender () 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< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QStateMachine class provides a hierarchical finite state machine.

Note
This class or function is reentrant.
Since
4.6

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:

QPushButton button;
QState *s1 = new QState();
s1->assignProperty(&button, "text", "Click me");
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 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.

See also
QAbstractState, QAbstractTransition, QState, {The State Machine Framework}

Definition at line 63 of file qstatemachine.h.

Enumerations

◆ 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.

  • NoError No error has occurred.
  • 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.
  • NoDefaultStateInHistoryStateError 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.
  • 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.
See also
setErrorState()
Enumerator
NoError 
NoInitialStateError 
NoDefaultStateInHistoryStateError 
NoCommonAncestorForTransitionError 

Definition at line 116 of file qstatemachine.h.

◆ 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.

  • NormalPriority The event has normal priority.
  • HighPriority The event has high priority.
Enumerator
NormalPriority 
HighPriority 

Definition at line 106 of file qstatemachine.h.

◆ RestorePolicy

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.

  • DontRestoreProperties The state machine should not save the initial values of properties and restore them later.
  • RestoreProperties The state machine should save the initial values of properties and restore them later.
See also
QStateMachine::globalRestorePolicy QState::assignProperty()
Enumerator
DontRestoreProperties 
RestoreProperties 

Definition at line 111 of file qstatemachine.h.

Constructors and Destructors

◆ QStateMachine() [1/2]

QStateMachine::QStateMachine ( QObject parent = 0)

Constructs a new state machine with the given parent.

Definition at line 1685 of file qstatemachine.cpp.

1686  : QState(*new QStateMachinePrivate, /*parentState=*/0)
1687 {
1688  // Can't pass the parent to the QState constructor, as it expects a QState
1689  // But this works as expected regardless of whether parent is a QState or not
1690  setParent(parent);
1691 }
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
QState(QState *parent=0)
Constructs a new state with the given parent state.
Definition: qstate.cpp:168

◆ ~QStateMachine()

QStateMachine::~QStateMachine ( )

Destroys this state machine.

Definition at line 1705 of file qstatemachine.cpp.

1706 {
1707 }

◆ QStateMachine() [2/2]

QStateMachine::QStateMachine ( QStateMachinePrivate dd,
QObject parent 
)
protected
Warning
This function is not part of the public interface.

Definition at line 1696 of file qstatemachine.cpp.

1697  : QState(dd, /*parentState=*/0)
1698 {
1699  setParent(parent);
1700 }
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
QState(QState *parent=0)
Constructs a new state with the given parent state.
Definition: qstate.cpp:168

Functions

◆ addDefaultAnimation()

void QStateMachine::addDefaultAnimation ( QAbstractAnimation animation)

Adds a default animation to be considered for any transition.

Definition at line 2244 of file qstatemachine.cpp.

2245 {
2246  Q_D(QStateMachine);
2247  d->defaultAnimations.append(animation);
2248 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ addState()

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()

Definition at line 1839 of file qstatemachine.cpp.

1840 {
1841  if (!state) {
1842  qWarning("QStateMachine::addState: cannot add null state");
1843  return;
1844  }
1845  if (QAbstractStatePrivate::get(state)->machine() == this) {
1846  qWarning("QStateMachine::addState: state has already been added to this machine");
1847  return;
1848  }
1849  state->setParent(this);
1850 }
QStateMachine * machine() const
Returns the state machine that this state is part of, or 0 if the state is not part of a state machin...
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
Q_CORE_EXPORT void qWarning(const char *,...)
static QAbstractStatePrivate * get(QAbstractState *q)

◆ beginMicrostep()

void QStateMachine::beginMicrostep ( QEvent event)
protectedvirtual

This function is called when the state machine is about to do a microstep.

Warning
This function is not part of the public interface.

The default implementation does nothing.

Definition at line 2173 of file qstatemachine.cpp.

2174 {
2175  Q_UNUSED(event);
2176 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ beginSelectTransitions()

void QStateMachine::beginSelectTransitions ( QEvent event)
protectedvirtual

This function is called when the state machine is about to select transitions based on the given event.

Warning
This function is not part of the public interface.

The default implementation does nothing.

Definition at line 2142 of file qstatemachine.cpp.

2143 {
2144  Q_UNUSED(event);
2145 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ cancelDelayedEvent()

bool QStateMachine::cancelDelayedEvent ( int  id)

Cancels the delayed event identified by the given id.

Note
This class or function is threadsafe.

The id should be a value returned by a call to postDelayedEvent(). Returns true if the event was successfully cancelled, otherwise returns false.

See also
postDelayedEvent()

Definition at line 2037 of file qstatemachine.cpp.

2038 {
2039  Q_D(QStateMachine);
2040  if (d->state != QStateMachinePrivate::Running) {
2041  qWarning("QStateMachine::cancelDelayedEvent: the machine is not running");
2042  return false;
2043  }
2044  QMutexLocker locker(&d->delayedEventsMutex);
2045  QEvent *e = d->delayedEvents.take(id);
2046  if (!e)
2047  return false;
2048  killTimer(id);
2049  delete e;
2050  return true;
2051 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ clearError()

void QStateMachine::clearError ( )

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

Definition at line 1800 of file qstatemachine.cpp.

1801 {
1802  Q_D(QStateMachine);
1803  d->errorString.clear();
1804  d->error = NoError;
1805 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ configuration()

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.

2061 {
2062  Q_D(const QStateMachine);
2063  return d->configuration;
2064 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ defaultAnimations()

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.

2254 {
2255  Q_D(const QStateMachine);
2256  return d->defaultAnimations;
2257 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ endMicrostep()

void QStateMachine::endMicrostep ( QEvent event)
protectedvirtual

This function is called when the state machine has finished doing a microstep.

Warning
This function is not part of the public interface.

The default implementation does nothing.

Definition at line 2189 of file qstatemachine.cpp.

2190 {
2191  Q_UNUSED(event);
2192 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ endSelectTransitions()

void QStateMachine::endSelectTransitions ( QEvent event)
protectedvirtual

This function is called when the state machine has finished selecting transitions based on the given event.

Warning
This function is not part of the public interface.

The default implementation does nothing.

Definition at line 2158 of file qstatemachine.cpp.

2159 {
2160  Q_UNUSED(event);
2161 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ error()

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.

1783 {
1784  Q_D(const QStateMachine);
1785  return d->error;
1786 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ errorString()

QString QStateMachine::errorString ( ) const

Referenced by error().

◆ event()

bool QStateMachine::event ( QEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QState.

Definition at line 2092 of file qstatemachine.cpp.

Referenced by postDelayedEvent(), and postEvent().

2093 {
2094  Q_D(QStateMachine);
2095  if (e->type() == QEvent::Timer) {
2096  QTimerEvent *te = static_cast<QTimerEvent*>(e);
2097  int tid = te->timerId();
2098  if (d->state != QStateMachinePrivate::Running) {
2099  // This event has been cancelled already
2100  QMutexLocker locker(&d->delayedEventsMutex);
2101  Q_ASSERT(!d->delayedEvents.contains(tid));
2102  return true;
2103  }
2104  d->delayedEventsMutex.lock();
2105  QEvent *ee = d->delayedEvents.take(tid);
2106  if (ee != 0) {
2107  killTimer(tid);
2108  d->delayedEventsMutex.unlock();
2109  d->postExternalEvent(ee);
2110  d->processEvents(QStateMachinePrivate::DirectProcessing);
2111  return true;
2112  } else {
2113  d->delayedEventsMutex.unlock();
2114  }
2115  }
2116  return QState::event(e);
2117 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
bool event(QEvent *e)
Reimplemented Function
Definition: qstate.cpp:509
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ eventFilter()

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

Reimplemented Function

Reimplemented from QObject.

Definition at line 2123 of file qstatemachine.cpp.

2124 {
2125  Q_D(QStateMachine);
2126  d->handleFilteredEvent(watched, event);
2127  return false;
2128 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ globalRestorePolicy()

QStateMachine::RestorePolicy QStateMachine::globalRestorePolicy ( ) const

Referenced by clearError().

◆ isAnimated()

bool QStateMachine::isAnimated ( ) const

Returns whether animations are enabled for this state machine.

Definition at line 2226 of file qstatemachine.cpp.

2227 {
2228  Q_D(const QStateMachine);
2229  return d->animated;
2230 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ isRunning()

bool QStateMachine::isRunning ( ) const

Returns whether this state machine is running.

start(), stop()

Definition at line 1878 of file qstatemachine.cpp.

1879 {
1880  Q_D(const QStateMachine);
1881  return (d->state == QStateMachinePrivate::Running);
1882 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ onEntry()

void QStateMachine::onEntry ( QEvent event)
protectedvirtual

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

Reimplemented Function

Reimplemented from QState.

Definition at line 2201 of file qstatemachine.cpp.

2202 {
2203  start();
2204  QState::onEntry(event);
2205 }
void onEntry(QEvent *event)
Reimplemented Function
Definition: qstate.cpp:446
void start()
Starts this state machine.

◆ onExit()

void QStateMachine::onExit ( QEvent event)
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.

2216 {
2217  stop();
2218  QState::onExit(event);
2219 }
void onExit(QEvent *event)
Reimplemented Function
Definition: qstate.cpp:454
void stop()
Stops this state machine.

◆ postDelayedEvent()

int QStateMachine::postDelayedEvent ( QEvent event,
int  delay 
)

Posts the given event for processing by this state machine, with the given delay in milliseconds.

Note
This class or function is threadsafe.

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
cancelDelayedEvent(), postEvent()

Definition at line 2001 of file qstatemachine.cpp.

2002 {
2003  Q_D(QStateMachine);
2004  if (d->state != QStateMachinePrivate::Running) {
2005  qWarning("QStateMachine::postDelayedEvent: cannot post event when the state machine is not running");
2006  return -1;
2007  }
2008  if (!event) {
2009  qWarning("QStateMachine::postDelayedEvent: cannot post null event");
2010  return -1;
2011  }
2012  if (delay < 0) {
2013  qWarning("QStateMachine::postDelayedEvent: delay cannot be negative");
2014  return -1;
2015  }
2016 #ifdef QSTATEMACHINE_DEBUG
2017  qDebug() << this << ": posting event" << event << "with delay" << delay;
2018 #endif
2019  QMutexLocker locker(&d->delayedEventsMutex);
2020  int tid = startTimer(delay);
2021  d->delayedEvents[tid] = event;
2022  return tid;
2023 }
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qDebug(const char *,...)
Q_CORE_EXPORT void qWarning(const char *,...)
bool event(QEvent *e)
Reimplemented Function
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ postEvent()

void QStateMachine::postEvent ( QEvent event,
EventPriority  priority = NormalPriority 
)

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

Note
This class or function is threadsafe.

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.

See also
postDelayedEvent()

Definition at line 1957 of file qstatemachine.cpp.

1958 {
1959  Q_D(QStateMachine);
1960  if (d->state != QStateMachinePrivate::Running) {
1961  qWarning("QStateMachine::postEvent: cannot post event when the state machine is not running");
1962  return;
1963  }
1964  if (!event) {
1965  qWarning("QStateMachine::postEvent: cannot post null event");
1966  return;
1967  }
1968 #ifdef QSTATEMACHINE_DEBUG
1969  qDebug() << this << ": posting event" << event;
1970 #endif
1971  switch (priority) {
1972  case NormalPriority:
1973  d->postExternalEvent(event);
1974  break;
1975  case HighPriority:
1976  d->postInternalEvent(event);
1977  break;
1978  }
1979  d->processEvents(QStateMachinePrivate::QueuedProcessing);
1980 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qDebug(const char *,...)
Q_CORE_EXPORT void qWarning(const char *,...)
bool event(QEvent *e)
Reimplemented Function

◆ removeDefaultAnimation()

void QStateMachine::removeDefaultAnimation ( QAbstractAnimation animation)

Removes animation from the list of default animations.

Definition at line 2262 of file qstatemachine.cpp.

2263 {
2264  Q_D(QStateMachine);
2265  d->defaultAnimations.removeAll(animation);
2266 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ removeState()

void QStateMachine::removeState ( QAbstractState state)

Removes the given state from this state machine.

The state machine releases ownership of the state.

See also
addState()

Definition at line 1858 of file qstatemachine.cpp.

1859 {
1860  if (!state) {
1861  qWarning("QStateMachine::removeState: cannot remove null state");
1862  return;
1863  }
1864  if (QAbstractStatePrivate::get(state)->machine() != this) {
1865  qWarning("QStateMachine::removeState: state %p's machine (%p)"
1866  " is different from this machine (%p)",
1867  state, QAbstractStatePrivate::get(state)->machine(), this);
1868  return;
1869  }
1870  state->setParent(0);
1871 }
QStateMachine * machine() const
Returns the state machine that this state is part of, or 0 if the state is not part of a state machin...
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
Q_CORE_EXPORT void qWarning(const char *,...)
static QAbstractStatePrivate * get(QAbstractState *q)

◆ setAnimated()

void QStateMachine::setAnimated ( bool  enabled)

Sets whether animations are enabled for this state machine.

Definition at line 2235 of file qstatemachine.cpp.

2236 {
2237  Q_D(QStateMachine);
2238  d->animated = enabled;
2239 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482
#define enabled

◆ setGlobalRestorePolicy()

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

Sets the restore policy of the state machine to restorePolicy.

The default restore policy is QAbstractState::DontRestoreProperties.

See also
globalRestorePolicy()

Definition at line 1824 of file qstatemachine.cpp.

1825 {
1826  Q_D(QStateMachine);
1827  d->globalRestorePolicy = restorePolicy;
1828 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ start

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()

Definition at line 1895 of file qstatemachine.cpp.

Referenced by onEntry().

1896 {
1897  Q_D(QStateMachine);
1898 
1899  if (initialState() == 0) {
1900  qWarning("QStateMachine::start: No initial state set for machine. Refusing to start.");
1901  return;
1902  }
1903 
1904  switch (d->state) {
1908  break;
1910  break;
1912  qWarning("QStateMachine::start(): already running");
1913  break;
1914  }
1915 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
QAbstractState * initialState() const

◆ started

QStateMachine::started ( )
signal

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

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

◆ stop

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()

Definition at line 1923 of file qstatemachine.cpp.

Referenced by onExit().

1924 {
1925  Q_D(QStateMachine);
1926  switch (d->state) {
1928  break;
1930  // the machine will exit as soon as it enters the event processing loop
1931  d->stop = true;
1932  break;
1934  d->stop = true;
1935  d->processEvents(QStateMachinePrivate::QueuedProcessing);
1936  break;
1937  }
1938 }
double d
Definition: qnumeric_p.h:62
The QStateMachine class provides a hierarchical finite state machine.
Definition: qstatemachine.h:63
#define Q_D(Class)
Definition: qglobal.h:2482

◆ stopped

QStateMachine::stopped ( )
signal

This signal is emitted when the state machine has stopped.

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

Properties

◆ animated

QStateMachine::animated
private

whether animations are enabled

The default value of this property is true.

See also
QAbstractTransition::addAnimation()

Definition at line 70 of file qstatemachine.h.

◆ errorString

QString QStateMachine::errorString
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.

◆ globalRestorePolicy

QStateMachine::RestorePolicy QStateMachine::globalRestorePolicy
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.

See also
setGlobalRestorePolicy()

Definition at line 67 of file qstatemachine.h.


The documentation for this class was generated from the following files: