Sophie

Sophie

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

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" />
<!-- pingpong.qdoc -->
  <title>Ping Pong States Example | Qt Widgets 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="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">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="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Ping Pong States Example</h1>
<span class="subtitle"></span>
<!-- $$$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&#x2e; 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">&gt;</span>postEvent(<span class="keyword">new</span> PingEvent());
          fprintf(stdout<span class="operator">,</span> <span class="string">&quot;ping?\n&quot;</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">&gt;</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">&gt;</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">&quot;pong!\n&quot;</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">&gt;</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">&gt;</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">&quot;ping?\n&quot;</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">&gt;</span>setObjectName(<span class="string">&quot;group&quot;</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">&gt;</span>setObjectName(<span class="string">&quot;pinger&quot;</span>);
      pinger<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>setObjectName(<span class="string">&quot;ponger&quot;</span>);
      ponger<span class="operator">-</span><span class="operator">&gt;</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">&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>