/usr/share/qt5/doc/qtdoc/qtquick-usecase-integratingjs.html is in qt5-doc-html 5.3.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- integratingjs.qdoc -->
<title>Use Case - Integrating JavaScript in QML | QtDoc 5.3</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li><a href="index.html">Qt 5.3</a></li>
<li>Use Case - Integrating JavaScript in QML</li>
<li id="buildversion">
Qt 5.3.2 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#using-javascript-expressions-for-property-values">Using JavaScript Expressions for Property Values</a></li>
<li class="level1"><a href="#adding-javascript-functions-in-qml">Adding JavaScript Functions in QML</a></li>
<li class="level1"><a href="#using-javascript-files">Using JavaScript files</a></li>
</ul>
</div>
<h1 class="title">Use Case - Integrating JavaScript in QML</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-usecase-integratingjs.html-description -->
<div class="descr"> <a name="details"></a>
<p>JavaScript code can be easily integrated into QML to provide UI logic, imperative control, or other benefits.</p>
<a name="using-javascript-expressions-for-property-values"></a>
<h2>Using JavaScript Expressions for Property Values</h2>
<p>JavaScript expressions can be used in QML as bindings. For example</p>
<pre class="cpp">Item {
width: Math<span class="operator">.</span>random()
height: width <span class="operator"><</span> <span class="number">100</span> <span class="operator">?</span> <span class="number">100</span> : (width <span class="operator">+</span> <span class="number">50</span>) <span class="operator">/</span> <span class="number">2</span>
}</pre>
<p>Note that function calls, like Math.random(), will not be revaluated unless their arguments change. So binding to Math.random() will be one random number and not revaluated, but if the width is changed in some other manner, the height binding will be reevaluated to take that into account.</p>
<a name="adding-javascript-functions-in-qml"></a>
<h2>Adding JavaScript Functions in QML</h2>
<p>JavaScript functions can be declared on QML items, like in the below example. This allows you to call the method using the item id.</p>
<pre class="qml">import QtQuick 2.3
<span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
<span class="name">id</span>: <span class="name">container</span>
<span class="name">width</span>: <span class="number">320</span>
<span class="name">height</span>: <span class="number">480</span>
<span class="keyword">function</span> <span class="name">randomNumber</span>() {
<span class="keyword">return</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">360</span>;
}
<span class="keyword">function</span> <span class="name">getNumber</span>() {
<span class="keyword">return</span> <span class="name">container</span>.<span class="name">randomNumber</span>();
}
<span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="comment">// This line uses the JS function from the item</span>
<span class="name">onClicked</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">container</span>.<span class="name">getNumber</span>();
}
<span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">color</span>: <span class="string">"#272822"</span>
<span class="name">width</span>: <span class="number">320</span>
<span class="name">height</span>: <span class="number">480</span>
}
<span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">rectangle</span>
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">width</span>: <span class="number">160</span>
<span class="name">height</span>: <span class="number">160</span>
<span class="name">color</span>: <span class="string">"green"</span>
Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
}
}</pre>
<a name="using-javascript-files"></a>
<h2>Using JavaScript files</h2>
<p>JavaScript files can be used for abstracting out logic from QML files. To do this, first place your functions inside a .js file like in the example shown.</p>
<pre class="js"><span class="comment">// myscript.js</span>
<span class="keyword">function</span> <span class="name">getRandom</span>(<span class="name">previousValue</span>) {
<span class="keyword">return</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">previousValue</span> <span class="operator">+</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">90</span>) <span class="operator">%</span> <span class="number">360</span>;
}</pre>
<p>Then import the file into any .qml file that needs to use the functions, like the example QML file below.</p>
<pre class="qml">import QtQuick 2.3
import "myscript.js" as Logic
<span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">320</span>
<span class="name">height</span>: <span class="number">480</span>
<span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">color</span>: <span class="string">"#272822"</span>
<span class="name">width</span>: <span class="number">320</span>
<span class="name">height</span>: <span class="number">480</span>
}
<span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="comment">// This line uses the JS function from the separate JS file</span>
<span class="name">onClicked</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">Logic</span>.<span class="name">getRandom</span>(<span class="name">rectangle</span>.<span class="name">rotation</span>);
}
<span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">rectangle</span>
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">width</span>: <span class="number">160</span>
<span class="name">height</span>: <span class="number">160</span>
<span class="name">color</span>: <span class="string">"green"</span>
Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
}
}</pre>
<p class="centerAlign"><img src="images/qml-uses-integratingjs.png" alt="" /></p><p>For further details on the JavaScript engine used by QML, as well as the difference from browser JS, see the full documentation on <tt>Using JavaScript Expressions with QML</tt>.</p>
</div>
<!-- @@@qtquick-usecase-integratingjs.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2014 Digia Plc and/or its
subsidiaries. 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> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|