<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- moveblocks.qdoc --> <title>Move Blocks Example | Qt Widgets 5.12.6</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.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Move Blocks Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></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="#the-main-function">The <code>main()</code> Function</a></li> <li class="level2"><a href="#the-creategeometrystate-function">The <code>createGeometryState()</code> 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> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Move Blocks Example</h1> <span class="subtitle"></span> <!-- $$$animation/moveblocks-brief --> <p>The Move Blocks example shows how to animate items in a <a href="qgraphicsscene.html">QGraphicsScene</a> using a <a href="../qtcore/qstatemachine.html">QStateMachine</a> with a custom transition.</p> <!-- @@@animation/moveblocks --> <!-- $$$animation/moveblocks-description --> <div class="descr"> <a name="details"></a> <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><code>StateSwitcher</code> inherits <a href="../qtcore/qstate.html">QState</a> and can add <code>StateSwitchTransition</code>s to other states. When entered, it will randomly transition to one of these states.</li> <li><code>StateSwitchTransition</code> is a custom transition that triggers on <code>StateSwitchEvent</code>s.</li> <li><code>StateSwitchEvent</code> is a <a href="../qtcore/qevent.html">QEvent</a> that triggers <code>StateSwitchTransition</code>s.</li> <li><code>QGraphicsRectWidget</code> is a <a href="graphicsview.html#qgraphicswidget">QGraphicsWidget</a> that simply paints its background in a solid <a href="../qtcore/qt.html#GlobalColor-enum">blue</a> color.</li> </ul> <p>The blocks are instances of <code>QGraphicsRectWidget</code> 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="../qtcore/qstatemachine.html">QStateMachine</a>. All this is done in <code>main()</code>. Let's look at the <code>main()</code> function first.</p> <a name="the-main-function"></a> <h2 id="the-main-function">The <code>main()</code> 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 <code>QGraphicsRectWidget</code>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">></span>setZValue(<span class="number">1</span>); button3<span class="operator">-</span><span class="operator">></span>setZValue(<span class="number">2</span>); button4<span class="operator">-</span><span class="operator">></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="../qtcore/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 <code>group</code> 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 <code>group</code> state:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstatemachine.html">QStateMachine</a></span> machine; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span>(); group<span class="operator">-</span><span class="operator">></span>setObjectName(<span class="string">"group"</span>); <span class="type"><a href="../qtcore/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="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>connect(group<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> <span class="operator">&</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 <code>group</code> is entered. As we will see later, <code>group</code> has a transition back to the <code>StateSwitcher</code> when the timer times out. <code>group</code> 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="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state1; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state2; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state3; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state4; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state5; <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state6; <span class="type"><a href="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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">></span>setInitialState(state1); </pre> <p><code>createGeometryState()</code> returns a <a href="../qtcore/qstate.html">QState</a> that will set the geometry of our items upon entry. It also assigns <code>group</code> as the parent of this state.</p> <p>A <a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a> inserted into a transition will use the values assigned to a <a href="../qtcore/qstate.html">QState</a> (with <a href="../qtcore/qstate.html#assignProperty">QState::assignProperty</a>()), i.e., 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="../qtcore/qparallelanimationgroup.html">QParallelAnimationGroup</a></span> animationGroup; <span class="type"><a href="../qtcore/qsequentialanimationgroup.html">QSequentialAnimationGroup</a></span> <span class="operator">*</span>subGroup; <span class="type"><a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a></span> <span class="operator">*</span>anim <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a></span>(button4<span class="operator">,</span> <span class="string">"geometry"</span>); anim<span class="operator">-</span><span class="operator">></span>setDuration(<span class="number">1000</span>); anim<span class="operator">-</span><span class="operator">></span>setEasingCurve(<span class="type"><a href="../qtcore/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 <code>animationGroup</code>, 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="../qtcore/qsequentialanimationgroup.html">QSequentialAnimationGroup</a></span>(<span class="operator">&</span>animationGroup); subGroup<span class="operator">-</span><span class="operator">></span>addPause(<span class="number">100</span>); anim <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a></span>(button3<span class="operator">,</span> <span class="string">"geometry"</span>); anim<span class="operator">-</span><span class="operator">></span>setDuration(<span class="number">1000</span>); anim<span class="operator">-</span><span class="operator">></span>setEasingCurve(<span class="type"><a href="../qtcore/qeasingcurve.html">QEasingCurve</a></span><span class="operator">::</span>OutElastic); subGroup<span class="operator">-</span><span class="operator">></span>addAnimation(anim); </pre> <p>The sequential animation group, <code>subGroup</code>, 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">&</span>machine); stateSwitcher<span class="operator">-</span><span class="operator">></span>setObjectName(<span class="string">"stateSwitcher"</span>); group<span class="operator">-</span><span class="operator">></span>addTransition(<span class="operator">&</span>timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> stateSwitcher); stateSwitcher<span class="operator">-</span><span class="operator">></span>addState(state1<span class="operator">,</span> <span class="operator">&</span>animationGroup); stateSwitcher<span class="operator">-</span><span class="operator">></span>addState(state2<span class="operator">,</span> <span class="operator">&</span>animationGroup); ... stateSwitcher<span class="operator">-</span><span class="operator">></span>addState(state7<span class="operator">,</span> <span class="operator">&</span>animationGroup); </pre> <p>A StateSwitchTransition is added to the state switcher in <code>StateSwitcher::addState()</code>. We also add the animation in this function. Since <a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a> uses the values from the states, we can insert the same <a href="../qtcore/qpropertyanimation.html">QPropertyAnimation</a> instance in all <code>StateSwitchTransition</code>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-creategeometrystate-function"></a> <h3 >The <code>createGeometryState()</code> Function</h3> <p>In <code>createGeometryState()</code>, we set up the geometry for each graphics item.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>createGeometryState(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>w1<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> <span class="operator">&</span>rect1<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>w2<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> <span class="operator">&</span>rect2<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>w3<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> <span class="operator">&</span>rect3<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>w4<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> <span class="operator">&</span>rect4<span class="operator">,</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>parent) { <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>result <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span>(parent); result<span class="operator">-</span><span class="operator">></span>assignProperty(w1<span class="operator">,</span> <span class="string">"geometry"</span><span class="operator">,</span> rect1); result<span class="operator">-</span><span class="operator">></span>assignProperty(w2<span class="operator">,</span> <span class="string">"geometry"</span><span class="operator">,</span> rect2); result<span class="operator">-</span><span class="operator">></span>assignProperty(w3<span class="operator">,</span> <span class="string">"geometry"</span><span class="operator">,</span> rect3); result<span class="operator">-</span><span class="operator">></span>assignProperty(w4<span class="operator">,</span> <span class="string">"geometry"</span><span class="operator">,</span> rect4); <span class="keyword">return</span> result; } </pre> <p>As mentioned before, <a href="../qtcore/qabstracttransition.html">QAbstractTransition</a> will set up an animation added with <a href="../qtcore/qabstracttransition.html#addAnimation">addAnimation()</a> using property values set with <a href="../qtcore/qstate.html#assignProperty">assignProperty()</a>.</p> <a name="the-stateswitcher-class"></a> <h2 id="the-stateswitcher-class">The StateSwitcher Class</h2> <p><code>StateSwitcher</code> has state switch transitions to each <a href="../qtcore/qstate.html">QState</a>s we created with <code>createGeometryState()</code>. Its job is to transition to one of these states at random when it is entered.</p> <p>All functions in <code>StateSwitcher</code> 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="../qtcore/qstate.html">QState</a></span> { Q_OBJECT <span class="keyword">public</span>: StateSwitcher(<span class="type"><a href="../qtcore/qstatemachine.html">QStateMachine</a></span> <span class="operator">*</span>machine) : <span class="type"><a href="../qtcore/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><code>StateSwitcher</code> is a state designed for a particular purpose and will always be a top-level state. We use <code>m_stateCount</code> to keep track of how many states we are managing, and <code>m_lastIndex</code> to remember which state was the last state to which we transitioned.</p> <pre class="cpp"> <span class="type">void</span> onEntry(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>) override { <span class="type">int</span> n; <span class="keyword">while</span> ((n <span class="operator">=</span> <span class="type"><a href="../qtcore/qrandomgenerator.html">QRandomGenerator</a></span><span class="operator">::</span>global()<span class="operator">-</span><span class="operator">></span>bounded(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">></span>postEvent(<span class="keyword">new</span> StateSwitchEvent(n)); } <span class="type">void</span> onExit(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>) override {} </pre> <p>We select the next state we are going to transition to, and post a <code>StateSwitchEvent</code>, which we know will trigger the <code>StateSwitchTransition</code> to the selected state.</p> <pre class="cpp"> <span class="type">void</span> addState(<span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>state<span class="operator">,</span> <span class="type"><a href="../qtcore/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">></span>setTargetState(state); addTransition(trans); trans<span class="operator">-</span><span class="operator">></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 id="the-stateswitchtransition-class">The StateSwitchTransition Class</h2> <p><code>StateSwitchTransition</code> inherits <a href="../qtcore/qabstracttransition.html">QAbstractTransition</a> and triggers on <code>StateSwitchEvent</code>s. It contains only inline functions, so let's take a look at its <a href="../qtcore/qabstracttransition.html#eventTest">eventTest()</a> function, which is the only function that we define..</p> <pre class="cpp"> bool eventTest(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>event) override { <span class="keyword">return</span> (event<span class="operator">-</span><span class="operator">></span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>Type(StateSwitchEvent<span class="operator">::</span>StateSwitchType)) <span class="operator">&</span><span class="operator">&</span> (<span class="keyword">static_cast</span><span class="operator"><</span>StateSwitchEvent <span class="operator">*</span><span class="operator">></span>(event)<span class="operator">-</span><span class="operator">></span>rand() <span class="operator">=</span><span class="operator">=</span> m_rand); } </pre> <p><code>eventTest</code> is called by <a href="../qtcore/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 id="the-stateswitchevent-class">The StateSwitchEvent Class</h2> <p><code>StateSwitchEvent</code> inherits <a href="../qtcore/qevent.html">QEvent</a>, and holds a number that has been assigned to a state and state switch transition by <code>StateSwitcher</code>. We have already seen how it is used to trigger <code>StateSwitchTransition</code>s in <code>StateSwitcher</code>.</p> <pre class="cpp"> <span class="keyword">class</span> StateSwitchEvent: <span class="keyword">public</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> { <span class="keyword">public</span>: StateSwitchEvent() : <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span>(Type(StateSwitchType)) { } <span class="keyword">explicit</span> StateSwitchEvent(<span class="type">int</span> rand) : <span class="type"><a href="../qtcore/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="../qtcore/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 id="the-qgraphicsrectwidget-class">The QGraphicsRectWidget Class</h2> <p>QGraphicsRectWidget inherits <a href="graphicsview.html#qgraphicswidget">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="../qtgui/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>) override { painter<span class="operator">-</span><span class="operator">></span>fillRect(rect()<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>blue); } }; </pre> <a name="moving-on"></a> <h2 id="moving-on">Moving On</h2> <p>The technique shown in this example works equally well for all <a href="../qtcore/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="../qtcore/qabstractanimation.html">QAbstractAnimation</a>, so any type of animation can be inserted into the graph.</p> <p>Files:</p> <ul> <li><a href="qtwidgets-animation-moveblocks-main-cpp.html">animation/moveblocks/main.cpp</a></li> <li><a href="qtwidgets-animation-moveblocks-moveblocks-pro.html">animation/moveblocks/moveblocks.pro</a></li> </ul> </div> <!-- @@@animation/moveblocks --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 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>