<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qquickworkerscript.cpp --> <title>WorkerScript QML Type | Qt QML 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="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >WorkerScript QML Type</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="#properties">Properties</a></li> <li class="level1"><a href="#signals">Signals</a></li> <li class="level1"><a href="#methods">Methods</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">WorkerScript QML Type</h1> <span class="subtitle"></span> <!-- $$$WorkerScript-brief --> <p>Enables the use of threads in a Qt Quick application <a href="#details">More...</a></p> <!-- @@@WorkerScript --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Import Statement:</td><td class="memItemRight bottomAlign"> import .</td></tr></table></div><ul> <li><a href="qml-workerscript-members.html">List of all members, including inherited members</a></li> </ul> <a name="properties"></a> <h2 id="properties">Properties</h2> <ul> <li class="fn"><b><b><a href="qml-workerscript.html#source-prop">source</a></b></b> : url</li> </ul> <a name="signals"></a> <h2 id="signals">Signals</h2> <ul> <li class="fn"><b><b><a href="qml-workerscript.html#message-signal">message</a></b></b>(jsobject <i>msg</i>)</li> </ul> <a name="methods"></a> <h2 id="methods">Methods</h2> <ul> <li class="fn"><b><b><a href="qml-workerscript.html#sendMessage-method">sendMessage</a></b></b>(jsobject <i>message</i>)</li> </ul> <!-- $$$WorkerScript-description --> <a name="details"></a> <h2 id="details">Detailed Description</h2> <p>Use <a href="qml-workerscript.html">WorkerScript</a> to run operations in a new thread. This is useful for running operations in the background so that the main GUI thread is not blocked.</p> <p>Messages can be passed between the new thread and the parent thread using <a href="qml-workerscript.html#sendMessage-method">sendMessage()</a> and the <code>onMessage()</code> handler.</p> <p>An example:</p> <pre class="qml"> import QtQuick 2.0 <span class="type">Rectangle</span> { <span class="name">width</span>: <span class="number">300</span>; <span class="name">height</span>: <span class="number">300</span> <span class="type">Text</span> { <span class="name">id</span>: <span class="name">myText</span> <span class="name">text</span>: <span class="string">'Click anywhere'</span> } <span class="type"><a href="qml-workerscript.html">WorkerScript</a></span> { <span class="name">id</span>: <span class="name">myWorker</span> <span class="name">source</span>: <span class="string">"script.js"</span> <span class="name">onMessage</span>: <span class="name">myText</span>.<span class="name">text</span> <span class="operator">=</span> <span class="name">messageObject</span>.<span class="name">reply</span> } <span class="type">MouseArea</span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">onClicked</span>: <span class="name">myWorker</span>.<span class="name">sendMessage</span>({ 'x': <span class="name">mouse</span>.<span class="name">x</span>, 'y': <span class="name">mouse</span>.<span class="name">y</span> }) } } </pre> <p>The above worker script specifies a JavaScript file, "script.js", that handles the operations to be performed in the new thread. Here is <code>script.js</code>:</p> <pre class="cpp"> <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">// ... long-running operations and calculations are done here</span> <span class="name">WorkerScript</span>.<span class="name">sendMessage</span>({ 'reply': <span class="string">'Mouse is at '</span> <span class="operator">+</span> <span class="name">message</span>.<span class="name">x</span> <span class="operator">+</span> <span class="string">','</span> <span class="operator">+</span> <span class="name">message</span>.<span class="name">y</span> }) } </pre> <p>When the user clicks anywhere within the rectangle, <code>sendMessage()</code> is called, triggering the <code>WorkerScript.onMessage()</code> handler in <code>script.js</code>. This in turn sends a reply message that is then received by the <code>onMessage()</code> handler of <code>myWorker</code>.</p> <a name="restrictions"></a> <h4 >Restrictions</h4> <p>Since the <code>WorkerScript.onMessage()</code> function is run in a separate thread, the JavaScript file is evaluated in a context separate from the main QML engine. This means that unlike an ordinary JavaScript file that is imported into QML, the <code>script.js</code> in the above example cannot access the properties, methods or other attributes of the QML item, nor can it access any context properties set on the QML object through <a href="qqmlcontext.html">QQmlContext</a>.</p> <p>Additionally, there are restrictions on the types of values that can be passed to and from the worker script. See the <a href="qml-workerscript.html#sendMessage-method">sendMessage()</a> documentation for details.</p> <p>Worker script can not use <a href="qtqml-javascript-imports.html">.import</a> syntax.</p> <p><b>See also </b>Qt Quick Examples - Threading and Threaded ListModel Example.</p> <!-- @@@WorkerScript --> <h2>Property Documentation</h2> <!-- $$$source --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="source-prop"> <td class="tblQmlPropNode"><p> <a name="source-prop"></a><span class="name">source</span> : <span class="type"><a href="qml-url.html">url</a></span></p></td></tr> </table></div> </div><div class="qmldoc"><p>This holds the url of the JavaScript file that implements the <code>WorkerScript.onMessage()</code> handler for threaded operations.</p> </div></div><!-- @@@source --> <br/> <h2>Signal Documentation</h2> <!-- $$$message --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="message-signal"> <td class="tblQmlFuncNode"><p> <a name="message-signal"></a><span class="name">message</span>(<span class="type">jsobject</span> <i>msg</i>)</p></td></tr> </table></div> </div><div class="qmldoc"><p>This signal is emitted when a message <i>msg</i> is received from a worker script in another thread through a call to <a href="qml-workerscript.html#sendMessage-method">sendMessage()</a>.</p> <p>The corresponding handler is <code>onMessage</code>.</p> </div></div><!-- @@@message --> <br/> <h2>Method Documentation</h2> <!-- $$$sendMessage --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="sendMessage-method"> <td class="tblQmlFuncNode"><p> <a name="sendMessage-method"></a><span class="name">sendMessage</span>(<span class="type">jsobject</span> <i>message</i>)</p></td></tr> </table></div> </div><div class="qmldoc"><p>Sends the given <i>message</i> to a worker script handler in another thread. The other worker script handler can receive this message through the onMessage() handler.</p> <p>The <code>message</code> object may only contain values of the following types:</p> <ul> <li>boolean, number, string</li> <li>JavaScript objects and arrays</li> <li><a href="qml-qtqml-models-listmodel.html">ListModel</a> objects (any other type of QObject* is not allowed)</li> </ul> <p>All objects and arrays are copied to the <code>message</code>. With the exception of <a href="qml-qtqml-models-listmodel.html">ListModel</a> objects, any modifications by the other thread to an object passed in <code>message</code> will not be reflected in the original object.</p> </div></div><!-- @@@sendMessage --> <br/> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</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>