Sophie

Sophie

distrib > Mageia > 6 > x86_64 > by-pkgid > 2cba8df17162abb32fcb8e6852f3eacc > files > 1629

qtdeclarative5-doc-5.9.4-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" />
<!-- threading.qdoc -->
  <title>Qt Quick Examples - Threading | Qt Quick 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="qtquick-index.html">Qt Quick</a></td><td >Qt Quick Examples - Threading</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="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#threaded-listmodel">Threaded ListModel</a></li>
<li class="level1"><a href="#workerscript">WorkerScript</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Examples - Threading</h1>
<span class="subtitle"></span>
<!-- $$$threading-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qml-threading-example.png" alt="" /></p><p><i>Threading</i> is a collection of QML multithreading examples.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="threaded-listmodel"></a>
<h2 id="threaded-listmodel">Threaded ListModel</h2>
<p><i>Threaded ListModel</i> contains a <a href="qml-qtquick-listview.html">ListView</a> and a ListModel. The ListModel object is updated asynchronously in another thread, and the results propagate back to the main thread. A timer requests updates from the worker thread periodically:</p>
<pre class="qml">

          <span class="type">Timer</span> {
              <span class="name">id</span>: <span class="name">timer</span>
              <span class="name">interval</span>: <span class="number">2000</span>; <span class="name">repeat</span>: <span class="number">true</span>
              <span class="name">running</span>: <span class="number">true</span>
              <span class="name">triggeredOnStart</span>: <span class="number">true</span>

              <span class="name">onTriggered</span>: {
                  var <span class="name">msg</span> = {'action': <span class="string">'appendCurrentTime'</span>, 'model': <span class="name">listModel</span>};
                  <span class="name">worker</span>.<span class="name">sendMessage</span>(<span class="name">msg</span>);
              }
          }

</pre>
<p>Inside the worker thread, the ListModel is synchronized once the data is finished loading:</p>
<pre class="js">

  <span class="name">WorkerScript</span>.<span class="name">onMessage</span> <span class="operator">=</span> <span class="keyword">function</span>(<span class="name">msg</span>) {
      <span class="keyword">if</span> (<span class="name">msg</span>.<span class="name">action</span> <span class="operator">==</span> <span class="string">'appendCurrentTime'</span>) {
          var <span class="name">data</span> = {'time': new <span class="name">Date</span>().<span class="name">toTimeString</span>()};
          <span class="name">msg</span>.<span class="name">model</span>.<span class="name">append</span>(<span class="name">data</span>);
          <span class="name">msg</span>.<span class="name">model</span>.<span class="name">sync</span>();   <span class="comment">// updates the changes to the list</span>
      }
  }

</pre>
<a name="workerscript"></a>
<h2 id="workerscript">WorkerScript</h2>
<p><i>WorkerScript</i> contains an example of using a <a href="qtquick-threading-example.html#workerscript">WorkerScript</a> to offload expensive calculations into another thread. This keeps the UI from being blocked. This example calculates numbers in Pascal's Triangle, and not in a very optimal way, so it will often take several seconds to complete the calculation. By doing this in a <a href="qtquick-threading-example.html#workerscript">WorkerScript</a> in another thread, the UI is not blocked during this time.</p>
<p>When the UI needs another value, a request is sent to the <a href="qtquick-threading-example.html#workerscript">WorkerScript</a>:</p>
<pre class="qml">

          <span class="type">Spinner</span> {
              <span class="name">id</span>: <span class="name">rowSpinner</span>
              <span class="name">label</span>: <span class="string">&quot;Row&quot;</span>
              <span class="name">onValueChanged</span>: {
                  <span class="name">resultText</span>.<span class="name">text</span> <span class="operator">=</span> <span class="string">&quot;Loading...&quot;</span>;
                  <span class="name">myWorker</span>.<span class="name">sendMessage</span>( { row: <span class="name">rowSpinner</span>.<span class="name">value</span>, column: <span class="name">columnSpinner</span>.<span class="name">value</span> } );
              }
          }

</pre>
<p>The workerscript then is free to take a really long time to calculate it:</p>
<pre class="js">

  <span class="name">WorkerScript</span>.<span class="name">onMessage</span> <span class="operator">=</span> <span class="keyword">function</span>(<span class="name">message</span>) {
      <span class="comment">//Calculate result (may take a while, using a naive algorithm)</span>
      var <span class="name">calculatedResult</span> = <span class="name">triangle</span>(<span class="name">message</span>.<span class="name">row</span>, <span class="name">message</span>.<span class="name">column</span>);
      <span class="comment">//Send result back to main thread</span>
      <span class="name">WorkerScript</span>.<span class="name">sendMessage</span>( { row: <span class="name">message</span>.<span class="name">row</span>,
                                  column: <span class="name">message</span>.<span class="name">column</span>,
                                  result: <span class="name">calculatedResult</span>} );
  }

</pre>
<p>When it's done, the result returns to the main scene via the <a href="qtquick-threading-example.html#workerscript">WorkerScript</a> type:</p>
<pre class="qml">

      <span class="type">WorkerScript</span> {
          <span class="name">id</span>: <span class="name">myWorker</span>
          <span class="name">source</span>: <span class="string">&quot;workerscript.js&quot;</span>

          <span class="name">onMessage</span>: {
              <span class="keyword">if</span> (<span class="name">messageObject</span>.<span class="name">row</span> <span class="operator">==</span> <span class="name">rowSpinner</span>.<span class="name">value</span> <span class="operator">&amp;&amp;</span> <span class="name">messageObject</span>.<span class="name">column</span> <span class="operator">==</span> <span class="name">columnSpinner</span>.<span class="name">value</span>){ <span class="comment">//Not an old result</span>
                  <span class="keyword">if</span> (<span class="name">messageObject</span>.<span class="name">result</span> <span class="operator">==</span> -<span class="number">1</span>)
                      <span class="name">resultText</span>.<span class="name">text</span> <span class="operator">=</span> <span class="string">&quot;Column must be &lt;= Row&quot;</span>;
                  <span class="keyword">else</span>
                      <span class="name">resultText</span>.<span class="name">text</span> <span class="operator">=</span> <span class="name">messageObject</span>.<span class="name">result</span>;
              }
          }
      }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtquick-threading-threading-qml.html">threading/threading.qml</a></li>
<li><a href="qtquick-threading-threadedlistmodel-dataloader-js.html">threading/threadedlistmodel/dataloader.js</a></li>
<li><a href="qtquick-threading-threadedlistmodel-timedisplay-qml.html">threading/threadedlistmodel/timedisplay.qml</a></li>
<li><a href="qtquick-threading-workerscript-spinner-qml.html">threading/workerscript/Spinner.qml</a></li>
<li><a href="qtquick-threading-workerscript-workerscript-js.html">threading/workerscript/workerscript.js</a></li>
<li><a href="qtquick-threading-workerscript-workerscript-qml.html">threading/workerscript/workerscript.qml</a></li>
<li><a href="qtquick-threading-main-cpp.html">threading/main.cpp</a></li>
<li><a href="qtquick-threading-threading-pro.html">threading/threading.pro</a></li>
<li><a href="qtquick-threading-threading-qmlproject.html">threading/threading.qmlproject</a></li>
<li><a href="qtquick-threading-threading-qrc.html">threading/threading.qrc</a></li>
<li><a href="qtquick-threading-threadedlistmodel-threadedlistmodel-qmlproject.html">threading/threadedlistmodel/threadedlistmodel.qmlproject</a></li>
<li><a href="qtquick-threading-workerscript-workerscript-qmlproject.html">threading/workerscript/workerscript.qmlproject</a></li>
</ul>
</div>
<!-- @@@threading -->
        </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>