Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 888

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- statemachine.qdoc -->
  <title>The State Machine Framework | Qt Core 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtcore-index.html">Qt Core</a></td><td >The State Machine Framework</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#classes-in-the-state-machine-framework">Classes in the State Machine Framework</a></li>
<li class="level1"><a href="#a-simple-state-machine">A Simple State Machine</a></li>
<li class="level1"><a href="#doing-useful-work-on-state-entry-and-exit">Doing Useful Work on State Entry and Exit</a></li>
<li class="level1"><a href="#state-machines-that-finish">State Machines That Finish</a></li>
<li class="level1"><a href="#sharing-transitions-by-grouping-states">Sharing Transitions By Grouping States</a></li>
<li class="level1"><a href="#using-history-states-to-save-and-restore-the-current-state">Using History States to Save and Restore the Current State</a></li>
<li class="level1"><a href="#using-parallel-states-to-avoid-a-combinatorial-explosion-of-states">Using Parallel States to Avoid a Combinatorial Explosion of States</a></li>
<li class="level1"><a href="#detecting-that-a-composite-state-has-finished">Detecting that a Composite State has Finished</a></li>
<li class="level1"><a href="#targetless-transitions">Targetless Transitions</a></li>
<li class="level1"><a href="#events-transitions-and-guards">Events, Transitions and Guards</a></li>
<li class="level1"><a href="#using-restore-policy-to-automatically-restore-properties">Using Restore Policy To Automatically Restore Properties</a></li>
<li class="level1"><a href="#animating-property-assignments">Animating Property Assignments</a></li>
<li class="level1"><a href="#detecting-that-all-properties-have-been-set-in-a-state">Detecting That All Properties Have Been Set In A State</a></li>
<li class="level1"><a href="#what-happens-if-a-state-is-exited-before-the-animation-has-finished">What Happens If A State Is Exited Before The Animation Has Finished</a></li>
<li class="level1"><a href="#default-animations">Default Animations</a></li>
<li class="level1"><a href="#nesting-state-machines">Nesting State Machines</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">The State Machine Framework</h1>
<span class="subtitle"></span>
<!-- $$$statemachine-api.html-description -->
<div class="descr"> <a name="details"></a>
<p>The State Machine framework provides classes for creating and executing state graphs. The concepts and notation are based on those from Harel's <a href="http://www.wisdom.weizmann.ac.il/~dharel/SCANNED.PAPERS/Statecharts.pdf">Statecharts: A visual formalism for complex systems</a>, which is also the basis of UML state diagrams. The semantics of state machine execution are based on State Chart XML (SCXML).</p>
<p>Statecharts provide a graphical way of modeling how a system reacts to stimuli. This is done by defining the possible <i>states</i> that the system can be in, and how the system can move from one state to another (<i>transitions</i> between states). A key characteristic of event-driven systems (such as Qt applications) is that behavior often depends not only on the last or current event, but also the events that preceded it. With statecharts, this information is easy to express.</p>
<p>The State Machine framework provides an API and execution model that can be used to effectively embed the elements and semantics of statecharts in Qt applications. The framework integrates tightly with Qt's meta-object system; for example, transitions between states can be triggered by signals, and states can be configured to set properties and invoke methods on {<a href="qobject.html">QObject</a>}s. Qt's event system is used to drive the state machines.</p>
<p>The state graph in the State Machine framework is hierarchical. States can be nested inside of other states, and the current configuration of the state machine consists of the set of states which are currently active. All the states in a valid configuration of the state machine will have a common ancestor.</p>
<a name="classes-in-the-state-machine-framework"></a>
<h2 id="classes-in-the-state-machine-framework">Classes in the State Machine Framework</h2>
<p>These classes are provided by qt for creating event-driven state machines.</p>
<div class="table"><table class="annotated">
<tr class="odd topAlign"><td class="tblName"><p><a href="qabstractstate.html">QAbstractState</a></p></td><td class="tblDescr"><p>The base class of states of a QStateMachine</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qabstracttransition.html">QAbstractTransition</a></p></td><td class="tblDescr"><p>The base class of transitions between QAbstractState objects</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qeventtransition.html">QEventTransition</a></p></td><td class="tblDescr"><p>QObject-specific transition for Qt events</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qfinalstate.html">QFinalState</a></p></td><td class="tblDescr"><p>Final state</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qhistorystate.html">QHistoryState</a></p></td><td class="tblDescr"><p>Means of returning to a previously active substate</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qsignaltransition.html">QSignalTransition</a></p></td><td class="tblDescr"><p>Transition based on a Qt signal</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qstate.html">QState</a></p></td><td class="tblDescr"><p>General-purpose state for QStateMachine</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qstatemachine.html">QStateMachine</a></p></td><td class="tblDescr"><p>Hierarchical finite state machine</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qstatemachine-signalevent.html">QStateMachine::SignalEvent</a></p></td><td class="tblDescr"><p>Represents a Qt signal event</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qstatemachine-wrappedevent.html">QStateMachine::WrappedEvent</a></p></td><td class="tblDescr"><p>Inherits QEvent and holds a clone of an event associated with a QObject</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtwidgets/qkeyeventtransition.html">QKeyEventTransition</a></p></td><td class="tblDescr"><p>Transition for key events</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtwidgets/qmouseeventtransition.html">QMouseEventTransition</a></p></td><td class="tblDescr"><p>Transition for mouse events</p></td></tr>
</table></div>
<a name="a-simple-state-machine"></a>
<h2 id="a-simple-state-machine">A Simple State Machine</h2>
<p>To demonstrate the core functionality of the State Machine API, let's look at a small example: A state machine with three states, <code>s1</code>, <code>s2</code> and <code>s3</code>. The state machine is controlled by a single <a href="../qtwidgets/qpushbutton.html">QPushButton</a>; when the button is clicked, the machine transitions to another state. Initially, the state machine is in state <code>s1</code>. The statechart for this machine is as follows:</p>
<p class="centerAlign"><img src="images/statemachine-button.png" alt="" /></p><p>The following snippet shows the code needed to create such a state machine. First, we create the state machine and states:</p>
<pre class="cpp">

      <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

</pre>
<p>Then, we create the transitions by using the <a href="qstate.html#addTransition">QState::addTransition</a>() function:</p>
<pre class="cpp">

      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);
      s2<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s3);
      s3<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s1);

</pre>
<p>Next, we add the states to the machine and set the machine's initial state:</p>
<pre class="cpp">

      machine<span class="operator">.</span>addState(s1);
      machine<span class="operator">.</span>addState(s2);
      machine<span class="operator">.</span>addState(s3);
      machine<span class="operator">.</span>setInitialState(s1);

</pre>
<p>Finally, we start the state machine:</p>
<pre class="cpp">

      machine<span class="operator">.</span>start();

</pre>
<p>The state machine executes asynchronously, i.e&#x2e; it becomes part of your application's event loop.</p>
<a name="doing-useful-work-on-state-entry-and-exit"></a>
<h2 id="doing-useful-work-on-state-entry-and-exit">Doing Useful Work on State Entry and Exit</h2>
<p>The above state machine merely transitions from one state to another, it doesn't perform any operations. The <a href="qstate.html#assignProperty">QState::assignProperty</a>() function can be used to have a state set a property of a <a href="qobject.html">QObject</a> when the state is entered. In the following snippet, the value that should be assigned to a <a href="../qtwidgets/qlabel.html">QLabel</a>'s text property is specified for each state:</p>
<pre class="cpp">

      s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(label<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;In state s1&quot;</span>);
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(label<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;In state s2&quot;</span>);
      s3<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(label<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;In state s3&quot;</span>);

</pre>
<p>When any of the states is entered, the label's text will be changed accordingly.</p>
<p>The <a href="qabstractstate.html#entered">QState::entered</a>() signal is emitted when the state is entered, and the <a href="qabstractstate.html#exited">QState::exited</a>() signal is emitted when the state is exited. In the following snippet, the button's <a href="../qtwidgets/qwidget.html#showMaximized">showMaximized()</a> slot will be called when state <code>s3</code> is entered, and the button's <a href="../qtwidgets/qwidget.html#showMinimized">showMinimized()</a> slot will be called when <code>s3</code> is exited:</p>
<pre class="cpp">

      <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(s3<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> button<span class="operator">,</span> SLOT(showMaximized()));
      <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(s3<span class="operator">,</span> SIGNAL(exited())<span class="operator">,</span> button<span class="operator">,</span> SLOT(showMinimized()));

</pre>
<p>Custom states can reimplement <a href="qabstractstate.html#onEntry">QAbstractState::onEntry</a>() and <a href="qabstractstate.html#onExit">QAbstractState::onExit</a>().</p>
<a name="state-machines-that-finish"></a>
<h2 id="state-machines-that-finish">State Machines That Finish</h2>
<p>The state machine defined in the previous section never finishes. In order for a state machine to be able to finish, it needs to have a top-level <i>final</i> state (<a href="qfinalstate.html">QFinalState</a> object). When the state machine enters a top-level final state, the machine will emit the <a href="qstate.html#finished">QStateMachine::finished</a>() signal and halt.</p>
<p>All you need to do to introduce a final state in the graph is create a <a href="qfinalstate.html">QFinalState</a> object and use it as the target of one or more transitions.</p>
<a name="sharing-transitions-by-grouping-states"></a>
<h2 id="sharing-transitions-by-grouping-states">Sharing Transitions By Grouping States</h2>
<p>Assume we wanted the user to be able to quit the application at any time by clicking a Quit button. In order to achieve this, we need to create a final state and make it the target of a transition associated with the Quit button's <a href="../qtwidgets/qabstractbutton.html#clicked">clicked()</a> signal. We could add a transition from each of <code>s1</code>, <code>s2</code> and <code>s3</code>; however, this seems redundant, and one would also have to remember to add such a transition from every new state that is added in the future.</p>
<p>We can achieve the same behavior (namely that clicking the Quit button quits the state machine, regardless of which state the state machine is in) by grouping states <code>s1</code>, <code>s2</code> and <code>s3</code>. This is done by creating a new top-level state and making the three original states children of the new state. The following diagram shows the new state machine.</p>
<p class="centerAlign"><img src="images/statemachine-button-nested.png" alt="" /></p><p>The three original states have been renamed <code>s11</code>, <code>s12</code> and <code>s13</code> to reflect that they are now children of the new top-level state, <code>s1</code>. Child states implicitly inherit the transitions of their parent state. This means it is now sufficient to add a single transition from <code>s1</code> to the final state <code>s2</code>. New states added to <code>s1</code> will also automatically inherit this transition.</p>
<p>All that's needed to group states is to specify the proper parent when the state is created. You also need to specify which of the child states is the initial one (i.e&#x2e; which child state the state machine should enter when the parent state is the target of a transition).</p>
<pre class="cpp">

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s11 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s12 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s13 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);
      s1<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(s11);
      machine<span class="operator">.</span>addState(s1);
      <span class="type"><a href="qfinalstate.html">QFinalState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qfinalstate.html">QFinalState</a></span>();
      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);
      machine<span class="operator">.</span>addState(s2);
      machine<span class="operator">.</span>setInitialState(s1);

      <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&amp;</span>machine<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> <span class="type"><a href="../qtwidgets/qapplication.html">QApplication</a></span><span class="operator">::</span>instance()<span class="operator">,</span> SLOT(quit()));

</pre>
<p>In this case we want the application to quit when the state machine is finished, so the machine's <a href="qstate.html#finished">finished()</a> signal is connected to the application's <a href="qcoreapplication.html#quit">quit()</a> slot.</p>
<p>A child state can override an inherited transition. For example, the following code adds a transition that effectively causes the Quit button to be ignored when the state machine is in state <code>s12</code>.</p>
<pre class="cpp">

      s12<span class="operator">-</span><span class="operator">&gt;</span>addTransition(quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s12);

</pre>
<p>A transition can have any state as its target, i.e&#x2e; the target state does not have to be on the same level in the state hierarchy as the source state.</p>
<a name="using-history-states-to-save-and-restore-the-current-state"></a>
<h2 id="using-history-states-to-save-and-restore-the-current-state">Using History States to Save and Restore the Current State</h2>
<p>Imagine that we wanted to add an &quot;interrupt&quot; mechanism to the example discussed in the previous section; the user should be able to click a button to have the state machine perform some non-related task, after which the state machine should resume whatever it was doing before (i.e&#x2e; return to the old state, which is one of <code>s11</code>, <code>s12</code> and <code>s13</code> in this case).</p>
<p>Such behavior can easily be modeled using <i>history states</i>. A history state (<a href="qhistorystate.html">QHistoryState</a> object) is a pseudo-state that represents the child state that the parent state was in the last time the parent state was exited.</p>
<p>A history state is created as a child of the state for which we wish to record the current child state; when the state machine detects the presence of such a state at runtime, it automatically records the current (real) child state when the parent state is exited. A transition to the history state is in fact a transition to the child state that the state machine had previously saved; the state machine automatically &quot;forwards&quot; the transition to the real child state.</p>
<p>The following diagram shows the state machine after the interrupt mechanism has been added.</p>
<p class="centerAlign"><img src="images/statemachine-button-history.png" alt="" /></p><p>The following code shows how it can be implemented; in this example we simply display a message box when <code>s3</code> is entered, then immediately return to the previous child state of <code>s1</code> via the history state.</p>
<pre class="cpp">

      <span class="type"><a href="qhistorystate.html">QHistoryState</a></span> <span class="operator">*</span>s1h <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhistorystate.html">QHistoryState</a></span>(s1);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      s3<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(label<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;In s3&quot;</span>);
      <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span> <span class="operator">*</span>mbox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span>(mainWindow);
      mbox<span class="operator">-</span><span class="operator">&gt;</span>addButton(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Ok);
      mbox<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;Interrupted!&quot;</span>);
      mbox<span class="operator">-</span><span class="operator">&gt;</span>setIcon(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Information);
      <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(s3<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> mbox<span class="operator">,</span> SLOT(exec()));
      s3<span class="operator">-</span><span class="operator">&gt;</span>addTransition(s1h);
      machine<span class="operator">.</span>addState(s3);

      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(interruptButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s3);

</pre>
<a name="using-parallel-states-to-avoid-a-combinatorial-explosion-of-states"></a>
<h2 id="using-parallel-states-to-avoid-a-combinatorial-explosion-of-states">Using Parallel States to Avoid a Combinatorial Explosion of States</h2>
<p>Assume that you wanted to model a set of mutually exclusive properties of a car in a single state machine. Let's say the properties we are interested in are Clean vs Dirty, and Moving vs Not moving. It would take four mutually exclusive states and eight transitions to be able to represent and freely move between all possible combinations.</p>
<p class="centerAlign"><img src="images/statemachine-nonparallel.png" alt="" /></p><p>If we added a third property (say, Red vs Blue), the total number of states would double, to eight; and if we added a fourth property (say, Enclosed vs Convertible), the total number of states would double again, to 16.</p>
<p>Using parallel states, the total number of states and transitions grows linearly as we add more properties, instead of exponentially. Furthermore, states can be added to or removed from the parallel state without affecting any of their sibling states.</p>
<p class="centerAlign"><img src="images/statemachine-parallel.png" alt="" /></p><p>To create a parallel state group, pass <a href="qstate.html#ChildMode-enum">QState::ParallelStates</a> to the <a href="qstate.html">QState</a> constructor.</p>
<pre class="cpp">

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(<span class="type"><a href="qstate.html">QState</a></span><span class="operator">::</span>ParallelStates);
      <span class="comment">// s11 and s12 will be entered in parallel</span>
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s11 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s12 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);

</pre>
<p>When a parallel state group is entered, all its child states will be simultaneously entered. Transitions within the individual child states operate normally. However, any of the child states may take a transition which exits the parent state. When this happens, the parent state and all of its child states are exited.</p>
<p>The parallelism in the State Machine framework follows an interleaved semantics. All parallel operations will be executed in a single, atomic step of the event processing, so no event can interrupt the parallel operations. However, events will still be processed sequentially, since the machine itself is single threaded. As an example: Consider the situation where there are two transitions that exit the same parallel state group, and their conditions become true simultaneously. In this case, the event that is processed last of the two will not have any effect, since the first event will already have caused the machine to exit from the parallel state.</p>
<a name="detecting-that-a-composite-state-has-finished"></a>
<h2 id="detecting-that-a-composite-state-has-finished">Detecting that a Composite State has Finished</h2>
<p>A child state can be final (a <a href="qfinalstate.html">QFinalState</a> object); when a final child state is entered, the parent state emits the <a href="qstate.html#finished">QState::finished</a>() signal. The following diagram shows a composite state <code>s1</code> which does some processing before entering a final state:</p>
<p class="centerAlign"><img src="images/statemachine-finished.png" alt="" /></p><p>When <code>s1</code> 's final state is entered, <code>s1</code> will automatically emit <a href="qstate.html#finished">finished()</a>. We use a signal transition to cause this event to trigger a state change:</p>
<pre class="cpp">

    s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(s1<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> s2);

</pre>
<p>Using final states in composite states is useful when you want to hide the internal details of a composite state; i.e&#x2e; the only thing the outside world should be able to do is enter the state, and get a notification when the state has completed its work. This is a very powerful abstraction and encapsulation mechanism when building complex (deeply nested) state machines. (In the above example, you could of course create a transition directly from <code>s1</code> 's <code>done</code> state rather than relying on <code>s1</code> 's <a href="qstate.html#finished">finished()</a> signal, but with the consequence that implementation details of <code>s1</code> are exposed and depended on).</p>
<p>For parallel state groups, the <a href="qstate.html#finished">QState::finished</a>() signal is emitted when <i>all</i> the child states have entered final states.</p>
<a name="targetless-transitions"></a>
<h2 id="targetless-transitions">Targetless Transitions</h2>
<p>A transition need not have a target state. A transition without a target can be triggered the same way as any other transition; the difference is that when a targetless transition is triggered, it doesn't cause any state changes. This allows you to react to a signal or event when your machine is in a certain state, without having to leave that state. Example:</p>
<pre class="cpp">

  <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
  <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(<span class="operator">&amp;</span>machine);

  <span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span> button;
  <span class="type"><a href="qsignaltransition.html">QSignalTransition</a></span> <span class="operator">*</span>trans <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsignaltransition.html">QSignalTransition</a></span>(<span class="operator">&amp;</span>button<span class="operator">,</span> SIGNAL(clicked()));
  s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(trans);

  <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span> msgBox;
  msgBox<span class="operator">.</span>setText(<span class="string">&quot;The button was clicked; carry on.&quot;</span>);
  <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(trans<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> <span class="operator">&amp;</span>msgBox<span class="operator">,</span> SLOT(exec()));

  machine<span class="operator">.</span>setInitialState(s1);

</pre>
<p>The message box will be displayed each time the button is clicked, but the state machine will remain in its current state (s1). If the target state were explicitly set to s1, however, s1 would be exited and re-entered each time (e.g&#x2e; the <a href="qabstractstate.html#entered">QAbstractState::entered</a>() and <a href="qabstractstate.html#exited">QAbstractState::exited</a>() signals would be emitted).</p>
<a name="events-transitions-and-guards"></a>
<h2 id="events-transitions-and-guards">Events, Transitions and Guards</h2>
<p>A <a href="qstatemachine.html">QStateMachine</a> runs its own event loop. For signal transitions (<a href="qsignaltransition.html">QSignalTransition</a> objects), <a href="qstatemachine.html">QStateMachine</a> automatically posts a <a href="qstatemachine-signalevent.html">QStateMachine::SignalEvent</a> to itself when it intercepts the corresponding signal; similarly, for <a href="qobject.html">QObject</a> event transitions (<a href="qeventtransition.html">QEventTransition</a> objects) a <a href="qstatemachine-wrappedevent.html">QStateMachine::WrappedEvent</a> is posted.</p>
<p>You can post your own events to the state machine using <a href="qstatemachine.html#postEvent">QStateMachine::postEvent</a>().</p>
<p>When posting a custom event to the state machine, you typically also have one or more custom transitions that can be triggered from events of that type. To create such a transition, you subclass <a href="qabstracttransition.html">QAbstractTransition</a> and reimplement <a href="qabstracttransition.html#eventTest">QAbstractTransition::eventTest</a>(), where you check if an event matches your event type (and optionally other criteria, e.g&#x2e; attributes of the event object).</p>
<p>Here we define our own custom event type, <code>StringEvent</code>, for posting strings to the state machine:</p>
<pre class="cpp">

  <span class="keyword">struct</span> StringEvent : <span class="keyword">public</span> <span class="type"><a href="qevent.html">QEvent</a></span>
  {
      StringEvent(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>val)
      : <span class="type"><a href="qevent.html">QEvent</a></span>(<span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>Type(<span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>User<span class="operator">+</span><span class="number">1</span>))<span class="operator">,</span>
        value(val) {}

      <span class="type"><a href="qstring.html">QString</a></span> value;
  };

</pre>
<p>Next, we define a transition that only triggers when the event's string matches a particular string (a <i>guarded</i> transition):</p>
<pre class="cpp">

  <span class="keyword">class</span> StringTransition : <span class="keyword">public</span> <span class="type"><a href="qabstracttransition.html">QAbstractTransition</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      StringTransition(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>value)
          : m_value(value) {}

  <span class="keyword">protected</span>:
      <span class="keyword">virtual</span> bool eventTest(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>e)
      {
          <span class="keyword">if</span> (e<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>Type(<span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>User<span class="operator">+</span><span class="number">1</span>)) <span class="comment">// StringEvent</span>
              <span class="keyword">return</span> <span class="keyword">false</span>;
          StringEvent <span class="operator">*</span>se <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span>StringEvent<span class="operator">*</span><span class="operator">&gt;</span>(e);
          <span class="keyword">return</span> (m_value <span class="operator">=</span><span class="operator">=</span> se<span class="operator">-</span><span class="operator">&gt;</span>value);
      }

      <span class="keyword">virtual</span> <span class="type">void</span> onTransition(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>) {}

  <span class="keyword">private</span>:
      <span class="type"><a href="qstring.html">QString</a></span> m_value;
  };

</pre>
<p>In the <a href="qabstracttransition.html#eventTest">eventTest()</a> reimplementation, we first check if the event type is the desired one; if so, we cast the event to a <code>StringEvent</code> and perform the string comparison.</p>
<p>The following is a statechart that uses the custom event and transition:</p>
<p class="centerAlign"><img src="images/statemachine-customevents.png" alt="" /></p><p>Here's what the implementation of the statechart looks like:</p>
<pre class="cpp">

      <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qfinalstate.html">QFinalState</a></span> <span class="operator">*</span>done <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qfinalstate.html">QFinalState</a></span>();

      StringTransition <span class="operator">*</span>t1 <span class="operator">=</span> <span class="keyword">new</span> StringTransition(<span class="string">&quot;Hello&quot;</span>);
      t1<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(s2);
      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(t1);
      StringTransition <span class="operator">*</span>t2 <span class="operator">=</span> <span class="keyword">new</span> StringTransition(<span class="string">&quot;world&quot;</span>);
      t2<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(done);
      s2<span class="operator">-</span><span class="operator">&gt;</span>addTransition(t2);

      machine<span class="operator">.</span>addState(s1);
      machine<span class="operator">.</span>addState(s2);
      machine<span class="operator">.</span>addState(done);
      machine<span class="operator">.</span>setInitialState(s1);

</pre>
<p>Once the machine is started, we can post events to it.</p>
<pre class="cpp">

      machine<span class="operator">.</span>postEvent(<span class="keyword">new</span> StringEvent(<span class="string">&quot;Hello&quot;</span>));
      machine<span class="operator">.</span>postEvent(<span class="keyword">new</span> StringEvent(<span class="string">&quot;world&quot;</span>));

</pre>
<p>An event that is not handled by any relevant transition will be silently consumed by the state machine. It can be useful to group states and provide a default handling of such events; for example, as illustrated in the following statechart:</p>
<p class="centerAlign"><img src="images/statemachine-customevents2.png" alt="" /></p><p>For deeply nested statecharts, you can add such &quot;fallback&quot; transitions at the level of granularity that's most appropriate.</p>
<a name="using-restore-policy-to-automatically-restore-properties"></a>
<h2 id="using-restore-policy-to-automatically-restore-properties">Using Restore Policy To Automatically Restore Properties</h2>
<p>In some state machines it can be useful to focus the attention on assigning properties in states, not on restoring them when the state is no longer active. If you know that a property should always be restored to its initial value when the machine enters a state that does not explicitly give the property a value, you can set the global restore policy to <a href="qstate.html#RestorePolicy-enum">QStateMachine::RestoreProperties</a>.</p>
<pre class="cpp">

  <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
  machine<span class="operator">.</span>setGlobalRestorePolicy(<span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>RestoreProperties);

</pre>
<p>When this restore policy is set, the machine will automatically restore all properties. If it enters a state where a given property is not set, it will first search the hierarchy of ancestors to see if the property is defined there. If it is, the property will be restored to the value defined by the closest ancestor. If not, it will be restored to its initial value (i.e&#x2e; the value of the property before any property assignments in states were executed.)</p>
<p>Take the following code:</p>
<pre class="cpp">

      <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
      machine<span class="operator">.</span>setGlobalRestorePolicy(<span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>RestoreProperties);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(object<span class="operator">,</span> <span class="string">&quot;fooBar&quot;</span><span class="operator">,</span> <span class="number">1.0</span>);
      machine<span class="operator">.</span>addState(s1);
      machine<span class="operator">.</span>setInitialState(s1);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      machine<span class="operator">.</span>addState(s2);

</pre>
<p>Lets say the property <code>fooBar</code> is 0.0 when the machine starts. When the machine is in state <code>s1</code>, the property will be 1.0, since the state explicitly assigns this value to it. When the machine is in state <code>s2</code>, no value is explicitly defined for the property, so it will implicitly be restored to 0.0&#x2e;</p>
<p>If we are using nested states, the parent defines a value for the property which is inherited by all descendants that do not explicitly assign a value to the property.</p>
<pre class="cpp">

      <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
      machine<span class="operator">.</span>setGlobalRestorePolicy(<span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>RestoreProperties);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(object<span class="operator">,</span> <span class="string">&quot;fooBar&quot;</span><span class="operator">,</span> <span class="number">1.0</span>);
      machine<span class="operator">.</span>addState(s1);
      machine<span class="operator">.</span>setInitialState(s1);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(object<span class="operator">,</span> <span class="string">&quot;fooBar&quot;</span><span class="operator">,</span> <span class="number">2.0</span>);
      s1<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(s2);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(s1);

</pre>
<p>Here <code>s1</code> has two children: <code>s2</code> and <code>s3</code>. When <code>s2</code> is entered, the property <code>fooBar</code> will have the value 2.0, since this is explicitly defined for the state. When the machine is in state <code>s3</code>, no value is defined for the state, but <code>s1</code> defines the property to be 1.0, so this is the value that will be assigned to <code>fooBar</code>.</p>
<a name="animating-property-assignments"></a>
<h2 id="animating-property-assignments">Animating Property Assignments</h2>
<p>The State Machine API connects with the Animation API in Qt to allow automatically animating properties as they are assigned in states.</p>
<p>Say we have the following code:</p>
<pre class="cpp">

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

      s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>));
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">100</span>));

      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);

</pre>
<p>Here we define two states of a user interface. In <code>s1</code> the <code>button</code> is small, and in <code>s2</code> it is bigger. If we click the button to transition from <code>s1</code> to <code>s2</code>, the geometry of the button will be set immediately when a given state has been entered. If we want the transition to be smooth, however, all we need to do is make a <a href="qpropertyanimation.html">QPropertyAnimation</a> and add this to the transition object.</p>
<pre class="cpp">

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

      s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>));
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">100</span>));

      <span class="type"><a href="qsignaltransition.html">QSignalTransition</a></span> <span class="operator">*</span>transition <span class="operator">=</span> s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);
      transition<span class="operator">-</span><span class="operator">&gt;</span>addAnimation(<span class="keyword">new</span> <span class="type"><a href="qpropertyanimation.html">QPropertyAnimation</a></span>(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span>));

</pre>
<p>Adding an animation for the property in question means that the property assignment will no longer take immediate effect when the state has been entered. Instead, the animation will start playing when the state has been entered and smoothly animate the property assignment. Since we do not set the start value or end value of the animation, these will be set implicitly. The start value of the animation will be the property's current value when the animation starts, and the end value will be set based on the property assignments defined for the state.</p>
<p>If the global restore policy of the state machine is set to <a href="qstate.html#RestorePolicy-enum">QStateMachine::RestoreProperties</a>, it is possible to also add animations for the property restorations.</p>
<a name="detecting-that-all-properties-have-been-set-in-a-state"></a>
<h2 id="detecting-that-all-properties-have-been-set-in-a-state">Detecting That All Properties Have Been Set In A State</h2>
<p>When animations are used to assign properties, a state no longer defines the exact values that a property will have when the machine is in the given state. While the animation is running, the property can potentially have any value, depending on the animation.</p>
<p>In some cases, it can be useful to be able to detect when the property has actually been assigned the value defined by a state.</p>
<p>Say we have the following code:</p>
<pre class="cpp">

      <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span> <span class="operator">*</span>messageBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span>(mainWindow);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>addButton(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Ok);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;Button geometry has been set!&quot;</span>);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>setIcon(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Information);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>));
      connect(s2<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> messageBox<span class="operator">,</span> SLOT(exec()));

      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);

</pre>
<p>When <code>button</code> is clicked, the machine will transition into state <code>s2</code>, which will set the geometry of the button, and then pop up a message box to alert the user that the geometry has been changed.</p>
<p>In the normal case, where animations are not used, this will operate as expected. However, if an animation for the <code>geometry</code> of <code>button</code> is set on the transition between <code>s1</code> and <code>s2</code>, the animation will be started when <code>s2</code> is entered, but the <code>geometry</code> property will not actually reach its defined value before the animation is finished running. In this case, the message box will pop up before the geometry of the button has actually been set.</p>
<p>To ensure that the message box does not pop up until the geometry actually reaches its final value, we can use the state's <a href="qstate.html#propertiesAssigned">propertiesAssigned()</a> signal. The <a href="qstate.html#propertiesAssigned">propertiesAssigned()</a> signal will be emitted when the property is assigned its final value, whether this is done immediately or after the animation has finished playing.</p>
<pre class="cpp">

      <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span> <span class="operator">*</span>messageBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span>(mainWindow);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>addButton(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Ok);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;Button geometry has been set!&quot;</span>);
      messageBox<span class="operator">-</span><span class="operator">&gt;</span>setIcon(<span class="type"><a href="../qtwidgets/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Information);

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>));

      <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
      connect(s3<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> messageBox<span class="operator">,</span> SLOT(exec()));

      s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> s2);
      s2<span class="operator">-</span><span class="operator">&gt;</span>addTransition(s2<span class="operator">,</span> SIGNAL(propertiesAssigned())<span class="operator">,</span> s3);

</pre>
<p>In this example, when <code>button</code> is clicked, the machine will enter <code>s2</code>. It will remain in state <code>s2</code> until the <code>geometry</code> property has been set to <code>QRect(0, 0, 50, 50)</code>. Then it will transition into <code>s3</code>. When <code>s3</code> is entered, the message box will pop up. If the transition into <code>s2</code> has an animation for the <code>geometry</code> property, then the machine will stay in <code>s2</code> until the animation has finished playing. If there is no such animation, it will simply set the property and immediately enter state <code>s3</code>.</p>
<p>Either way, when the machine is in state <code>s3</code>, you are guaranteed that the property <code>geometry</code> has been assigned the defined value.</p>
<p>If the global restore policy is set to <a href="qstate.html#RestorePolicy-enum">QStateMachine::RestoreProperties</a>, the state will not emit the <a href="qstate.html#propertiesAssigned">propertiesAssigned()</a> signal until these have been executed as well.</p>
<a name="what-happens-if-a-state-is-exited-before-the-animation-has-finished"></a>
<h2 id="what-happens-if-a-state-is-exited-before-the-animation-has-finished">What Happens If A State Is Exited Before The Animation Has Finished</h2>
<p>If a state has property assignments, and the transition into the state has animations for the properties, the state can potentially be exited before the properties have been assigned to the values defines by the state. This is true in particular when there are transitions out from the state that do not depend on the <a href="qstate.html#propertiesAssigned">propertiesAssigned()</a> signal, as described in the previous section.</p>
<p>The State Machine API guarantees that a property assigned by the state machine either:</p>
<ul>
<li>Has a value explicitly assigned to the property.</li>
<li>Is currently being animated into a value explicitly assigned to the property.</li>
</ul>
<p>When a state is exited prior to the animation finishing, the behavior of the state machine depends on the target state of the transition. If the target state explicitly assigns a value to the property, no additional action will be taken. The property will be assigned the value defined by the target state.</p>
<p>If the target state does not assign any value to the property, there are two options: By default, the property will be assigned the value defined by the state it is leaving (the value it would have been assigned if the animation had been permitted to finish playing). If a global restore policy is set, however, this will take precedence, and the property will be restored as usual.</p>
<a name="default-animations"></a>
<h2 id="default-animations">Default Animations</h2>
<p>As described earlier, you can add animations to transitions to make sure property assignments in the target state are animated. If you want a specific animation to be used for a given property regardless of which transition is taken, you can add it as a default animation to the state machine. This is in particular useful when the properties assigned (or restored) by specific states is not known when the machine is constructed.</p>
<pre class="cpp">

  <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
  <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();

  s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(object<span class="operator">,</span> <span class="string">&quot;fooBar&quot;</span><span class="operator">,</span> <span class="number">2.0</span>);
  s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(s2);

  <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> machine;
  machine<span class="operator">.</span>setInitialState(s1);
  machine<span class="operator">.</span>addDefaultAnimation(<span class="keyword">new</span> <span class="type"><a href="qpropertyanimation.html">QPropertyAnimation</a></span>(object<span class="operator">,</span> <span class="string">&quot;fooBar&quot;</span>));

</pre>
<p>When the machine is in state <code>s2</code>, the machine will play the default animation for the property <code>fooBar</code> since this property is assigned by <code>s2</code>.</p>
<p>Note that animations explicitly set on transitions will take precedence over any default animation for the given property.</p>
<a name="nesting-state-machines"></a>
<h2 id="nesting-state-machines">Nesting State Machines</h2>
<p><a href="qstatemachine.html">QStateMachine</a> is a subclass of <a href="qstate.html">QState</a>. This allows for a state machine to be a child state of another machine. <a href="qstatemachine.html">QStateMachine</a> reimplements <a href="qstate.html#onEntry">QState::onEntry</a>() and calls <a href="qstatemachine.html#start">QStateMachine::start</a>(), so that when the child state machine is entered, it will automatically start running.</p>
<p>The parent state machine treats the child machine as an <i>atomic</i> state in the state machine algorithm. The child state machine is self-contained; it maintains its own event queue and configuration. In particular, note that the <a href="qstatemachine.html#configuration">configuration()</a> of the child machine is not part of the parent machine's configuration (only the child machine itself is).</p>
<p>States of the child state machine cannot be specified as targets of transitions in the parent state machine; only the child state machine itself can. Conversely, states of the parent state machine cannot be specified as targets of transitions in the child state machine. The child state machine's <a href="qstate.html#finished">finished</a>() signal can be used to trigger a transition in the parent machine.</p>
</div>
<p><b>See also </b>The Declarative State Machine Framework.</p>
<!-- @@@statemachine-api.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>