Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 1750

qtdeclarative5-doc-5.12.6-1.mga7.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.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="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"><a href="qtquick-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="#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-brief -->
<p>This is a collection of QML multithreading examples.</p>
<!-- @@@threading -->
<!-- $$$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">

          Timer {
              id: timer
              interval: 2000; repeat: true
              running: true
              triggeredOnStart: true

              onTriggered: {
                  var msg = {'action': 'appendCurrentTime', 'model': listModel};
                  worker.sendMessage(msg);
              }
          }

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

  WorkerScript.onMessage = function(msg) {
      if (msg.action == 'appendCurrentTime') {
          var data = {'time': new Date().toTimeString()};
          msg.model.append(data);
          msg.model.sync();   // updates the changes to the list
      }
  }

</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">

          Spinner {
              id: rowSpinner
              label: "Row"
              onValueChanged: {
                  resultText.text = "Loading...";
                  myWorker.sendMessage( { row: rowSpinner.value, column: columnSpinner.value } );
              }
          }

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

  WorkerScript.onMessage = function(message) {
      //Calculate result (may take a while, using a naive algorithm)
      var calculatedResult = triangle(message.row, message.column);
      //Send result back to main thread
      WorkerScript.sendMessage( { row: message.row,
                                  column: message.column,
                                  result: calculatedResult} );
  }

</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">

      WorkerScript {
          id: myWorker
          source: "workerscript.mjs"

          onMessage: {
              if (messageObject.row == rowSpinner.value && messageObject.column == columnSpinner.value){ //Not an old result
                  if (messageObject.result == -1)
                      resultText.text = "Column must be <= Row";
                  else
                      resultText.text = messageObject.result;
              }
          }
      }

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