Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 77

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- moveblocks.qdoc -->
  <title>Qt 4.8: Move Blocks Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Move Blocks Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#the-function">The <tt>main()</tt> Function</a></li>
<li class="level2"><a href="#the-function">The <tt>createGeometryState()</tt> Function</a></li>
<li class="level1"><a href="#the-stateswitcher-class">The StateSwitcher Class</a></li>
<li class="level1"><a href="#the-stateswitchtransition-class">The StateSwitchTransition Class</a></li>
<li class="level1"><a href="#the-stateswitchevent-class">The StateSwitchEvent Class</a></li>
<li class="level1"><a href="#the-qgraphicsrectwidget-class">The QGraphicsRectWidget Class</a></li>
<li class="level1"><a href="#moving-on">Moving On</a></li>
</ul>
</div>
<h1 class="title">Move Blocks Example</h1>
<span class="subtitle"></span>
<!-- $$$animation/moveblocks-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="animation-moveblocks-main-cpp.html">animation/moveblocks/main.cpp</a></li>
<li><a href="animation-moveblocks-moveblocks-pro.html">animation/moveblocks/moveblocks.pro</a></li>
</ul>
<p>The Move Blocks example shows how to animate items in a <a href="qgraphicsscene.html">QGraphicsScene</a> using a <a href="qstatemachine.html">QStateMachine</a> with a custom transition.<p class="centerAlign"><img src="images/moveblocks-example.png" alt="" /></p><p>The example animates the blue blocks that you can see in the image above. The animation moves the blocks between four preset positions.</p>
<p>The example consists of the following classes:</p>
<ul>
<li><tt>StateSwitcher</tt> inherits <a href="qstate.html">QState</a> and can add <tt>StateSwitchTransition</tt>s to other states. When entered, it will randomly transition to one of these states.</li>
<li><tt>StateSwitchTransition</tt> is a custom transition that triggers on <tt>StateSwitchEvent</tt>s.</li>
<li><tt>StateSwitchEvent</tt> is a <a href="qevent.html">QEvent</a> that triggers <tt>StateSwitchTransition</tt>s.</li>
<li><tt>QGraphicsRectWidget</tt> is a <a href="qgraphicswidget.html">QGraphicsWidget</a> that simply paints its background in a solid <a href="qt.html#GlobalColor-enum">blue</a> color.</li>
</ul>
<p>The blocks are instances of <tt>QGraphicsRectWidget</tt> and are animated in a <a href="qgraphicsscene.html">QGraphicsScene</a>. We do this by building a state graph, which we insert animations into. The graph is then executed in a <a href="qstatemachine.html">QStateMachine</a>. All this is done in <tt>main()</tt>. Let's look at the <tt>main()</tt> function first.</p>
<a name="the-function"></a>
<h2>The <tt>main()</tt> Function</h2>
<p>After <a href="qapplication.html">QApplication</a> has been initialized, we set up the <a href="qgraphicsscene.html">QGraphicsScene</a> with its <tt>QGraphicsRectWidget</tt>s.</p>
<pre class="cpp">     <span class="type">QGraphicsRectWidget</span> <span class="operator">*</span>button1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGraphicsRectWidget</span>;
     <span class="type">QGraphicsRectWidget</span> <span class="operator">*</span>button2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGraphicsRectWidget</span>;
     <span class="type">QGraphicsRectWidget</span> <span class="operator">*</span>button3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGraphicsRectWidget</span>;
     <span class="type">QGraphicsRectWidget</span> <span class="operator">*</span>button4 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGraphicsRectWidget</span>;
     button2<span class="operator">-</span><span class="operator">&gt;</span>setZValue(<span class="number">1</span>);
     button3<span class="operator">-</span><span class="operator">&gt;</span>setZValue(<span class="number">2</span>);
     button4<span class="operator">-</span><span class="operator">&gt;</span>setZValue(<span class="number">3</span>);
     <span class="type"><a href="qgraphicsscene.html">QGraphicsScene</a></span> scene(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">300</span><span class="operator">,</span> <span class="number">300</span>);
     scene<span class="operator">.</span>setBackgroundBrush(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>black);
     scene<span class="operator">.</span>addItem(button1);
     scene<span class="operator">.</span>addItem(button2);
     scene<span class="operator">.</span>addItem(button3);
     scene<span class="operator">.</span>addItem(button4);</pre>
<p>After adding the scene to a <a href="qgraphicsview.html">QGraphicsView</a>, it is time to build the state graph. Let's first look at a statechart of what we are trying to build.</p>
<p class="centerAlign"><img src="images/move-blocks-chart.png" alt="" /></p><p>Note that the <tt>group</tt> has seven sub states, but we have only included three of them in the diagram. The code that builds this graph will be examined line-by-line, and will show how the graph works. First off, we construct the <tt>group</tt> state:</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>group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
     group<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;group&quot;</span>);
     <span class="type"><a href="qtimer.html">QTimer</a></span> timer;
     timer<span class="operator">.</span>setInterval(<span class="number">1250</span>);
     timer<span class="operator">.</span>setSingleShot(<span class="keyword">true</span>);
     <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(group<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> <span class="operator">&amp;</span>timer<span class="operator">,</span> SLOT(start()));</pre>
<p>The timer is used to add a delay between each time the blocks are moved. The timer is started when <tt>group</tt> is entered. As we will see later, <tt>group</tt> has a transition back to the <tt>StateSwitcher</tt> when the timer times out. <tt>group</tt> is the initial state in the machine, so an animation will be scheduled when the example is started.</p>
<pre class="cpp">     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state1;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state2;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state3;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state4;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state5;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state6;
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state7;

     state1 <span class="operator">=</span> createGeometryState(button1<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">100</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="operator">,</span>
                                  button2<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">150</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="operator">,</span>
                                  button3<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">200</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="operator">,</span>
                                  button4<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">250</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="operator">,</span>
                                  group);
     ...
     state7 <span class="operator">=</span> createGeometryState(button1<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</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="operator">,</span>
                                  button2<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">250</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="operator">,</span>
                                  button3<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">250</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>)<span class="operator">,</span>
                                  button4<span class="operator">,</span> <span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">250</span><span class="operator">,</span> <span class="number">250</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">50</span>)<span class="operator">,</span>
                                  group);
     group<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(state1);</pre>
<p><tt>createGeometryState()</tt> returns a <a href="qstate.html">QState</a> that will set the geometry of our items upon entry. It also assigns <tt>group</tt> as the parent of this state.</p>
<p>A <a href="qpropertyanimation.html">QPropertyAnimation</a> inserted into a transition will use the values assigned to a <a href="qstate.html">QState</a> (with <a href="qstate.html#assignProperty">QState::assignProperty</a>()), i.e&#x2e;, the animation will interpolate between the current values of the properties and the values in the target state. We add animated transitions to the state graph later.</p>
<pre class="cpp">     <span class="type"><a href="qparallelanimationgroup.html">QParallelAnimationGroup</a></span> animationGroup;
     <span class="type"><a href="qsequentialanimationgroup.html">QSequentialAnimationGroup</a></span> <span class="operator">*</span>subGroup;

     <span class="type"><a href="qpropertyanimation.html">QPropertyAnimation</a></span> <span class="operator">*</span>anim <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpropertyanimation.html">QPropertyAnimation</a></span>(button4<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span>);
     anim<span class="operator">-</span><span class="operator">&gt;</span>setDuration(<span class="number">1000</span>);
     anim<span class="operator">-</span><span class="operator">&gt;</span>setEasingCurve(<span class="type"><a href="qeasingcurve.html">QEasingCurve</a></span><span class="operator">::</span>OutElastic);
     animationGroup<span class="operator">.</span>addAnimation(anim);</pre>
<p>We move the items in parallel. Each item is added to <tt>animationGroup</tt>, which is the animation that is inserted into the transitions.</p>
<pre class="cpp">     subGroup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsequentialanimationgroup.html">QSequentialAnimationGroup</a></span>(<span class="operator">&amp;</span>animationGroup);
     subGroup<span class="operator">-</span><span class="operator">&gt;</span>addPause(<span class="number">100</span>);
     anim <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpropertyanimation.html">QPropertyAnimation</a></span>(button3<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span>);
     anim<span class="operator">-</span><span class="operator">&gt;</span>setDuration(<span class="number">1000</span>);
     anim<span class="operator">-</span><span class="operator">&gt;</span>setEasingCurve(<span class="type"><a href="qeasingcurve.html">QEasingCurve</a></span><span class="operator">::</span>OutElastic);
     subGroup<span class="operator">-</span><span class="operator">&gt;</span>addAnimation(anim);</pre>
<p>The sequential animation group, <tt>subGroup</tt>, helps us insert a delay between the animation of each item.</p>
<pre class="cpp">     StateSwitcher <span class="operator">*</span>stateSwitcher <span class="operator">=</span> <span class="keyword">new</span> StateSwitcher(<span class="operator">&amp;</span>machine);
     stateSwitcher<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;stateSwitcher&quot;</span>);
     group<span class="operator">-</span><span class="operator">&gt;</span>addTransition(<span class="operator">&amp;</span>timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> stateSwitcher);
     stateSwitcher<span class="operator">-</span><span class="operator">&gt;</span>addState(state1<span class="operator">,</span> <span class="operator">&amp;</span>animationGroup);
     stateSwitcher<span class="operator">-</span><span class="operator">&gt;</span>addState(state2<span class="operator">,</span> <span class="operator">&amp;</span>animationGroup);
     ...
     stateSwitcher<span class="operator">-</span><span class="operator">&gt;</span>addState(state7<span class="operator">,</span> <span class="operator">&amp;</span>animationGroup);</pre>
<p>A StateSwitchTransition is added to the state switcher in <tt>StateSwitcher::addState()</tt>. We also add the animation in this function. Since <a href="qpropertyanimation.html">QPropertyAnimation</a> uses the values from the states, we can insert the same <a href="qpropertyanimation.html">QPropertyAnimation</a> instance in all <tt>StateSwitchTransition</tt>s.</p>
<p>As mentioned previously, we add a transition to the state switcher that triggers when the timer times out.</p>
<pre class="cpp">     machine<span class="operator">.</span>addState(group);
     machine<span class="operator">.</span>setInitialState(group);
     machine<span class="operator">.</span>start();</pre>
<p>Finally, we can create the state machine, add our initial state, and start execution of the state graph.</p>
<a name="the-function"></a>
<h3>The <tt>createGeometryState()</tt> Function</h3>
<p>In <tt>createGeometryState()</tt>, we set up the geometry for each graphics item.</p>
<pre class="cpp"> <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>createGeometryState(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>w1<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect1<span class="operator">,</span>
                             <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>w2<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect2<span class="operator">,</span>
                             <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>w3<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect3<span class="operator">,</span>
                             <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>w4<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect4<span class="operator">,</span>
                             <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>parent)
 {
     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>result <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(parent);
     result<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(w1<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> rect1);
     result<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(w2<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> rect2);
     result<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(w3<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> rect3);
     result<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(w4<span class="operator">,</span> <span class="string">&quot;geometry&quot;</span><span class="operator">,</span> rect4);

     <span class="keyword">return</span> result;
 }</pre>
<p>As mentioned before, <a href="qabstracttransition.html">QAbstractTransition</a> will set up an animation added with <a href="qabstracttransition.html#addAnimation">addAnimation()</a> using property values set with <a href="qstate.html#assignProperty">assignProperty()</a>.</p>
<a name="the-stateswitcher-class"></a>
<h2>The StateSwitcher Class</h2>
<p><tt>StateSwitcher</tt> has state switch transitions to each <a href="qstate.html">QState</a>s we created with <tt>createGeometryState()</tt>. Its job is to transition to one of these states at random when it is entered.</p>
<p>All functions in <tt>StateSwitcher</tt> are inlined. We'll step through its definition.</p>
<pre class="cpp"> <span class="keyword">class</span> StateSwitcher : <span class="keyword">public</span> <span class="type"><a href="qstate.html">QState</a></span>
 {
     Q_OBJECT
 <span class="keyword">public</span>:
     StateSwitcher(<span class="type"><a href="qstatemachine.html">QStateMachine</a></span> <span class="operator">*</span>machine)
         : <span class="type"><a href="qstate.html">QState</a></span>(machine)<span class="operator">,</span> m_stateCount(<span class="number">0</span>)<span class="operator">,</span> m_lastIndex(<span class="number">0</span>)
     { }</pre>
<p><tt>StateSwitcher</tt> is a state designed for a particular purpose and will always be a top-level state. We use <tt>m_stateCount</tt> to keep track of how many states we are managing, and <tt>m_lastIndex</tt> to remember which state was the last state to which we transitioned.</p>
<pre class="cpp">     <span class="keyword">virtual</span> <span class="type">void</span> onEntry(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>)
     {
         <span class="type">int</span> n;
         <span class="keyword">while</span> ((n <span class="operator">=</span> (qrand() <span class="operator">%</span> m_stateCount <span class="operator">+</span> <span class="number">1</span>)) <span class="operator">=</span><span class="operator">=</span> m_lastIndex)
         { }
         m_lastIndex <span class="operator">=</span> n;
         machine()<span class="operator">-</span><span class="operator">&gt;</span>postEvent(<span class="keyword">new</span> StateSwitchEvent(n));
     }
     <span class="keyword">virtual</span> <span class="type">void</span> onExit(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>) {}</pre>
<p>We select the next state we are going to transition to, and post a <tt>StateSwitchEvent</tt>, which we know will trigger the <tt>StateSwitchTransition</tt> to the selected state.</p>
<pre class="cpp">     <span class="type">void</span> addState(<span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>state<span class="operator">,</span> <span class="type"><a href="qabstractanimation.html">QAbstractAnimation</a></span> <span class="operator">*</span>animation) {
         StateSwitchTransition <span class="operator">*</span>trans <span class="operator">=</span> <span class="keyword">new</span> StateSwitchTransition(<span class="operator">+</span><span class="operator">+</span>m_stateCount);
         trans<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(state);
         addTransition(trans);
         trans<span class="operator">-</span><span class="operator">&gt;</span>addAnimation(animation);
     }</pre>
<p>This is where the magic happens. We assign a number to each state added. This number is given to both a StateSwitchTransition and to StateSwitchEvents. As we have seen, state switch events will trigger a transition with the same number.</p>
<a name="the-stateswitchtransition-class"></a>
<h2>The StateSwitchTransition Class</h2>
<p><tt>StateSwitchTransition</tt> inherits <a href="qabstracttransition.html">QAbstractTransition</a> and triggers on <tt>StateSwitchEvent</tt>s. It contains only inline functions, so let's take a look at its <a href="qabstracttransition.html#eventTest">eventTest()</a> function, which is the only function that we define..</p>
<pre class="cpp">     <span class="keyword">virtual</span> <span class="type">bool</span> eventTest(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event)
     {
         <span class="keyword">return</span> (event<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(StateSwitchEvent<span class="operator">::</span>StateSwitchType))
             <span class="operator">&amp;</span><span class="operator">&amp;</span> (<span class="keyword">static_cast</span><span class="operator">&lt;</span>StateSwitchEvent <span class="operator">*</span><span class="operator">&gt;</span>(event)<span class="operator">-</span><span class="operator">&gt;</span>rand() <span class="operator">=</span><span class="operator">=</span> m_rand);
     }</pre>
<p><tt>eventTest</tt> is called by <a href="qstatemachine.html">QStateMachine</a> when it checks whether a transition should be triggered--a return value of true means that it will. We simply check if our assigned number is equal to the event's number (in which case we fire away).</p>
<a name="the-stateswitchevent-class"></a>
<h2>The StateSwitchEvent Class</h2>
<p><tt>StateSwitchEvent</tt> inherits <a href="qevent.html">QEvent</a>, and holds a number that has been assigned to a state and state switch transition by <tt>StateSwitcher</tt>. We have already seen how it is used to trigger <tt>StateSwitchTransition</tt>s in <tt>StateSwitcher</tt>.</p>
<pre class="cpp"> <span class="keyword">class</span> StateSwitchEvent: <span class="keyword">public</span> <span class="type"><a href="qevent.html">QEvent</a></span>
 {
 <span class="keyword">public</span>:
     StateSwitchEvent()
         : <span class="type"><a href="qevent.html">QEvent</a></span>(Type(StateSwitchType))
     {
     }

     StateSwitchEvent(<span class="type">int</span> rand)
         : <span class="type"><a href="qevent.html">QEvent</a></span>(Type(StateSwitchType))<span class="operator">,</span>
           m_rand(rand)
     {
     }

     <span class="keyword">enum</span> { StateSwitchType <span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">256</span> };

     <span class="type">int</span> rand() <span class="keyword">const</span> { <span class="keyword">return</span> m_rand; }

 <span class="keyword">private</span>:
     <span class="type">int</span> m_rand;
 };</pre>
<p>We only have inlined functions in this class, so a look at its definition will do.</p>
<a name="the-qgraphicsrectwidget-class"></a>
<h2>The QGraphicsRectWidget Class</h2>
<p>QGraphicsRectWidget inherits <a href="qgraphicswidget.html">QGraphicsWidget</a> and simply paints its <a href="qwidget.html#rect-prop">rect()</a> blue. We inline <a href="qwidget.html#paintEvent">paintEvent()</a>, which is the only function we define. Here is the QGraphicsRectWidget class definition:</p>
<pre class="cpp"> <span class="keyword">class</span> <span class="type">QGraphicsRectWidget</span> : <span class="keyword">public</span> <span class="type"><a href="qgraphicswidget.html">QGraphicsWidget</a></span>
 {
 <span class="keyword">public</span>:
     <span class="type">void</span> paint(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstyleoptiongraphicsitem.html">QStyleOptionGraphicsItem</a></span> <span class="operator">*</span><span class="operator">,</span>
                <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>)
     {
         painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect()<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue);
     }
 };</pre>
<a name="moving-on"></a>
<h2>Moving On</h2>
<p>The technique shown in this example works equally well for all <a href="qpropertyanimation.html">QPropertyAnimation</a>s. As long as the value to be animated is a Qt property, you can insert an animation of it into a state graph.</p>
<p>QState::addAnimation() takes a <a href="qabstractanimation.html">QAbstractAnimation</a>, so any type of animation can be inserted into the graph.</p>
</div>
<!-- @@@animation/moveblocks -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      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.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>