<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- pingpong.qdoc --> <title>Ping Pong States 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 >Ping Pong States 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="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Ping Pong States Example</h1> <span class="subtitle"></span> <!-- $$$statemachine/pingpong-brief --> <p>The Ping Pong States example shows how to use parallel states together with custom events and transitions in <a href="../qtcore/statemachine-api.html">The State Machine Framework</a>.</p> <!-- @@@statemachine/pingpong --> <!-- $$$statemachine/pingpong-description --> <div class="descr"> <a name="details"></a> <p>This example implements a statechart where two states communicate by posting events to the state machine. The state chart looks as follows:</p> <p class="centerAlign"><img src="images/pingpong-example.png" alt="" /></p><p>The <code>pinger</code> and <code>ponger</code> states are parallel states, i.e. they are entered simultaneously and will take transitions independently of eachother.</p> <p>The <code>pinger</code> state will post the first <code>ping</code> event upon entry; the <code>ponger</code> state will respond by posting a <code>pong</code> event; this will cause the <code>pinger</code> state to post a new <code>ping</code> event; and so on.</p> <pre class="cpp"> <span class="keyword">class</span> PingEvent : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> { <span class="keyword">public</span>: PingEvent() : <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span>(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>Type(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>User<span class="operator">+</span><span class="number">2</span>)) {} }; <span class="keyword">class</span> PongEvent : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> { <span class="keyword">public</span>: PongEvent() : <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span>(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>Type(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>User<span class="operator">+</span><span class="number">3</span>)) {} }; </pre> <p>Two custom events are defined, <code>PingEvent</code> and <code>PongEvent</code>.</p> <pre class="cpp"> <span class="keyword">class</span> Pinger : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span> { <span class="keyword">public</span>: Pinger(<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>(parent) {} <span class="keyword">protected</span>: <span class="type">void</span> onEntry(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>) override { machine()<span class="operator">-</span><span class="operator">></span>postEvent(<span class="keyword">new</span> PingEvent()); fprintf(stdout<span class="operator">,</span> <span class="string">"ping?\n"</span>); } }; </pre> <p>The <code>Pinger</code> class defines a state that posts a <code>PingEvent</code> to the state machine when the state is entered.</p> <pre class="cpp"> <span class="keyword">class</span> PingTransition : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qabstracttransition.html">QAbstractTransition</a></span> { <span class="keyword">public</span>: PingTransition() {} <span class="keyword">protected</span>: bool eventTest(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>e) override { <span class="keyword">return</span> (e<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>User<span class="operator">+</span><span class="number">2</span>); } <span class="type">void</span> onTransition(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>) override { machine()<span class="operator">-</span><span class="operator">></span>postDelayedEvent(<span class="keyword">new</span> PongEvent()<span class="operator">,</span> <span class="number">500</span>); fprintf(stdout<span class="operator">,</span> <span class="string">"pong!\n"</span>); } }; </pre> <p>The <code>PingTransition</code> class defines a transition that is triggered by events of type <code>PingEvent</code>, and that posts a <code>PongEvent</code> (with a delay of 500 milliseconds) to the state machine when the transition is triggered.</p> <pre class="cpp"> <span class="keyword">class</span> PongTransition : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qabstracttransition.html">QAbstractTransition</a></span> { <span class="keyword">public</span>: PongTransition() {} <span class="keyword">protected</span>: bool eventTest(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>e) override { <span class="keyword">return</span> (e<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>User<span class="operator">+</span><span class="number">3</span>); } <span class="type">void</span> onTransition(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>) override { machine()<span class="operator">-</span><span class="operator">></span>postDelayedEvent(<span class="keyword">new</span> PingEvent()<span class="operator">,</span> <span class="number">500</span>); fprintf(stdout<span class="operator">,</span> <span class="string">"ping?\n"</span>); } }; </pre> <p>The <code>PongTransition</code> class defines a transition that is triggered by events of type <code>PongEvent</code>, and that posts a <code>PingEvent</code> (with a delay of 500 milliseconds) to the state machine when the transition is triggered.</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv) { <span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span> app(argc<span class="operator">,</span> argv); <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>(<span class="type"><a href="../qtcore/qstate.html">QState</a></span><span class="operator">::</span>ParallelStates); group<span class="operator">-</span><span class="operator">></span>setObjectName(<span class="string">"group"</span>); </pre> <p>The main() function begins by creating a state machine and a parallel state group.</p> <pre class="cpp"> Pinger <span class="operator">*</span>pinger <span class="operator">=</span> <span class="keyword">new</span> Pinger(group); pinger<span class="operator">-</span><span class="operator">></span>setObjectName(<span class="string">"pinger"</span>); pinger<span class="operator">-</span><span class="operator">></span>addTransition(<span class="keyword">new</span> PongTransition()); <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>ponger <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span>(group); ponger<span class="operator">-</span><span class="operator">></span>setObjectName(<span class="string">"ponger"</span>); ponger<span class="operator">-</span><span class="operator">></span>addTransition(<span class="keyword">new</span> PingTransition()); </pre> <p>Next, the <code>pinger</code> and <code>ponger</code> states are created, with the parallel state group as their parent state. Note that the transitions are <i>targetless</i>. When such a transition is triggered, the source state won't be exited and re-entered; only the transition's onTransition() function will be called, and the state machine's configuration will remain the same, which is precisely what we want in this case.</p> <pre class="cpp"> machine<span class="operator">.</span>addState(group); machine<span class="operator">.</span>setInitialState(group); machine<span class="operator">.</span>start(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <p>Finally, the group is added to the state machine, the machine is started, and the application event loop is entered.</p> <p>Files:</p> <ul> <li><a href="qtwidgets-statemachine-pingpong-main-cpp.html">statemachine/pingpong/main.cpp</a></li> <li><a href="qtwidgets-statemachine-pingpong-pingpong-pro.html">statemachine/pingpong/pingpong.pro</a></li> </ul> </div> <!-- @@@statemachine/pingpong --> </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>