This file is indexed.

/usr/share/qt5/doc/qtdoc/qtquick-usecase-integratingjs.html is in qt5-doc-html 5.9.5-0ubuntu1.

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- integratingjs.qdoc -->
  <title>Use Case - Integrating JavaScript in QML | Qt 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 ><a href="index.html">Qt 5.9</a></td><td >Use Case - Integrating JavaScript in QML</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 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="#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>
<div class="sidebar-content" id="sidebar-content"></div></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 id="using-javascript-expressions-for-property-values">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">&lt;</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 id="adding-javascript-functions-in-qml">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">&quot;#272822&quot;</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">&quot;green&quot;</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 id="using-javascript-files">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 &quot;myscript.js&quot; 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">&quot;#272822&quot;</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">&quot;green&quot;</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 <a href="../qtqml/qtqml-javascript-expressions.html">JavaScript Expressions in QML Documents</a>.</p>
</div>
<!-- @@@qtquick-usecase-integratingjs.html -->
        </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>