Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QStateMachine Class Reference
[QtCore module]

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

 #include <QStateMachine>

Inherits QState.

Note: All functions in this class are reentrant, but postEvent(), postDelayedEvent(), and cancelDelayedEvent() are also thread-safe.

This class was introduced in Qt 4.6.


Public Types

class SignalEvent
class WrappedEvent
enum Error { NoError, NoInitialStateError, NoDefaultStateInHistoryStateError, NoCommonAncestorForTransitionError }
enum EventPriority { NormalPriority, HighPriority }
enum RestorePolicy { DontRestoreProperties, RestoreProperties }

Properties

  • 3 properties inherited from QState
  • 1 property inherited from QObject

Public Functions

QStateMachine ( QObject * parent = 0 )
~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
QStateMachine::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 ( QStateMachine::RestorePolicy restorePolicy )

Reimplemented Public Functions

virtual bool eventFilter ( QObject * watched, QEvent * event )
  • 11 public functions inherited from QState
  • 2 public functions inherited from QAbstractState
  • 29 public functions inherited from QObject

Public Slots

void start ()
void stop ()
  • 1 public slot inherited from QObject

Signals

void started ()
void stopped ()

Reimplemented Protected Functions

virtual bool event ( QEvent * e )
virtual void onEntry ( QEvent * event )
virtual void onExit ( QEvent * event )
  • 3 protected functions inherited from QState
  • 3 protected functions inherited from QAbstractState
  • 7 protected functions inherited from QObject

Additional Inherited Members

  • 5 static public members inherited from QObject
  • 3 protected functions inherited from QState
  • 3 protected functions inherited from QAbstractState
  • 7 protected functions inherited from QObject

Detailed Description

The QStateMachine class provides a hierarchical finite state machine.

QStateMachine is based on the concepts and notation of Statecharts. QStateMachine is part of The State Machine Framework.

A state machine manages a set of states (classes that inherit from QAbstractState) and transitions (descendants 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 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;

 QStateMachine machine;
 QState *s1 = new QState();
 s1->assignProperty(&button, "text", "Click me");

 QFinalState *s2 = new QFinalState();
 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 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, and The State Machine Framework.


Member Type Documentation

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.

ConstantValueDescription
QStateMachine::NoError0No error has occurred.
QStateMachine::NoInitialStateError1The 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::NoDefaultStateInHistoryStateError2The 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::NoCommonAncestorForTransitionError3The 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().

enum QStateMachine::EventPriority

This enum type specifies the priority of an event posted to the state machine using postEvent().

Events of high priority are processed before events of normal priority.

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

enum QStateMachine::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.

ConstantValueDescription
QStateMachine::DontRestoreProperties0The state machine should not save the initial values of properties and restore them later.
QStateMachine::RestoreProperties1The state machine should save the initial values of properties and restore them later.

See also QStateMachine::globalRestorePolicy and QState::assignProperty().


Property Documentation

animated : bool

This property holds whether animations are enabled.

The default value of this property is true.

Access functions:

bool isAnimated () const
void setAnimated ( bool enabled )

See also QAbstractTransition::addAnimation().

errorString : const QString

This property holds the error string of this state machine.

Access functions:

QString errorString () const

globalRestorePolicy : RestorePolicy

This property holds the restore policy for states of this state machine.

The default value of this property is QStateMachine::DontRestoreProperties.

Access functions:

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

Member Function Documentation

QStateMachine::QStateMachine ( QObject * parent = 0 )

Constructs a new state machine with the given parent.

QStateMachine::~QStateMachine ()

Destroys this state machine.

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() and 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 cancelled, otherwise returns false.

Note: This function 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 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.

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.

bool QStateMachine::event ( QEvent * e )   [virtual protected]

Reimplemented from QObject::event().

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

Reimplemented from QObject::eventFilter().

bool QStateMachine::isRunning () const

Returns whether this state machine is running.

start(), stop()

void QStateMachine::onEntry ( QEvent * event )   [virtual protected]

Reimplemented from QAbstractState::onEntry().

void QStateMachine::onExit ( QEvent * event )   [virtual protected]

Reimplemented from QAbstractState::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 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.

Note: This function is thread-safe.

See also cancelDelayedEvent() and postEvent().

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.

Note: This function 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::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(), and initialState().

void QStateMachine::started ()   [signal]

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

See also QStateMachine::finished() and 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() and start().

void QStateMachine::stopped ()   [signal]

This signal is emitted when the state machine has stopped.

See also QStateMachine::stop() and QStateMachine::finished().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 103
  2. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 56
  3. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 90
  4. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 31
  5. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 11
Page suivante
  1. Linus Torvalds : le "C++ est un langage horrible", en justifiant le choix du C pour le système de gestion de version Git 100
  2. Comment prendre en compte l'utilisateur dans vos applications ? Pour un développeur, « 90 % des utilisateurs sont des idiots » 231
  3. Quel est LE livre que tout développeur doit lire absolument ? Celui qui vous a le plus marqué et inspiré 96
  4. Apple cède et s'engage à payer des droits à Nokia, le conflit des brevets entre les deux firmes s'achève 158
  5. Nokia porte à nouveau plainte contre Apple pour violation de sept nouveaux brevets 158
  6. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 103
  7. Quel est le code dont vous êtes le plus fier ? Pourquoi l'avez-vous écrit ? Et pourquoi vous a-t-il donné autant de satisfaction ? 83
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web