Sophie

Sophie

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

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" />
<!-- basictypes.qdoc -->
  <title>var QML Basic 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 >var QML Basic 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="#change-notification-semantics">Change Notification Semantics</a></li>
<li class="level1"><a href="#property-value-initialization-semantics">Property Value Initialization Semantics</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">var QML Basic Type</h1>
<span class="subtitle"></span>
<!-- $$$var-description -->
<div class="descr"> <a name="details"></a>
<p>a generic property type.</p>
<p>The <code>var</code> type is a generic property type that can refer to any data type.</p>
<p>It is equivalent to a regular JavaScript variable. For example, var properties can store numbers, strings, objects, arrays and functions:</p>
<pre class="qml">

  <span class="type">Item</span> {
      property <span class="type">var</span> <span class="name">aNumber</span>: <span class="number">100</span>
      property <span class="type">var</span> <span class="name">aBool</span>: <span class="number">false</span>
      property <span class="type">var</span> <span class="name">aString</span>: <span class="string">&quot;Hello world!&quot;</span>
      property <span class="type">var</span> <span class="name">anotherString</span>: <span class="name">String</span>(<span class="string">&quot;#FF008800&quot;</span>)
      property <span class="type">var</span> <span class="name">aColor</span>: <span class="name">Qt</span>.<span class="name">rgba</span>(<span class="number">0.2</span>, <span class="number">0.3</span>, <span class="number">0.4</span>, <span class="number">0.5</span>)
      property <span class="type">var</span> <span class="name">aRect</span>: <span class="name">Qt</span>.<span class="name">rect</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>)
      property <span class="type">var</span> <span class="name">aPoint</span>: <span class="name">Qt</span>.<span class="name">point</span>(<span class="number">10</span>, <span class="number">10</span>)
      property <span class="type">var</span> <span class="name">aSize</span>: <span class="name">Qt</span>.<span class="name">size</span>(<span class="number">10</span>, <span class="number">10</span>)
      property <span class="type">var</span> <span class="name">aVector3d</span>: <span class="name">Qt</span>.<span class="name">vector3d</span>(<span class="number">100</span>, <span class="number">100</span>, <span class="number">100</span>)
      property <span class="type">var</span> <span class="name">anArray</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&quot;four&quot;</span>, <span class="string">&quot;five&quot;</span>, (<span class="keyword">function</span>() { <span class="keyword">return</span> <span class="string">&quot;six&quot;</span>; })]
      property <span class="type">var</span> <span class="name">anObject</span>: { &quot;foo&quot;: <span class="number">10</span>, &quot;bar&quot;: <span class="number">20</span> }
      property <span class="type">var</span> <span class="name">aFunction</span>: (<span class="keyword">function</span>() { <span class="keyword">return</span> <span class="string">&quot;one&quot;</span>; })
  }

</pre>
<a name="change-notification-semantics"></a>
<h2 id="change-notification-semantics">Change Notification Semantics</h2>
<p>It is important to note that changes in regular properties of JavaScript objects assigned to a var property will <b>not</b> trigger updates of bindings that access them. The example below will display &quot;The car has 4 wheels&quot; as the change to the wheels property will not cause the reevaluation of the binding assigned to the &quot;text&quot; property:</p>
<pre class="qml">

  <span class="type">Item</span> {
      property <span class="type">var</span> <span class="name">car</span>: new <span class="name">Object</span>({wheels: <span class="number">4</span>})

      <span class="type">Text</span> {
          <span class="name">text</span>: <span class="string">&quot;The car has &quot;</span> <span class="operator">+</span> <span class="name">car</span>.<span class="name">wheels</span> <span class="operator">+</span> <span class="string">&quot; wheels&quot;</span>;
      }

      <span class="name">Component</span>.onCompleted: {
          <span class="name">car</span>.<span class="name">wheels</span> <span class="operator">=</span> <span class="number">6</span>;
      }
  }

</pre>
<p>If the onCompleted handler instead had <code>&quot;car = new Object({wheels: 6})&quot;</code> then the text would be updated to say &quot;The car has 6 wheels&quot;, since the car property itself would be changed, which causes a change notification to be emitted.</p>
<a name="property-value-initialization-semantics"></a>
<h2 id="property-value-initialization-semantics">Property Value Initialization Semantics</h2>
<p>The QML syntax defines that curly braces on the right-hand-side of a property value initialization assignment denote a binding assignment. This can be confusing when initializing a <code>var</code> property, as empty curly braces in JavaScript can denote either an expression block or an empty object declaration. If you wish to initialize a <code>var</code> property to an empty object value, you should wrap the curly braces in parentheses.</p>
<p>For example:</p>
<pre class="qml">

  <span class="type">Item</span> {
      property <span class="type">var</span> <span class="name">first</span>:  {}   <span class="comment">// nothing = undefined</span>
      property <span class="type">var</span> <span class="name">second</span>: {{}} <span class="comment">// empty expression block = undefined</span>
      property <span class="type">var</span> <span class="name">third</span>:  ({}) <span class="comment">// empty object</span>
  }

</pre>
<p>In the previous example, the <code>first</code> property is bound to an empty expression, whose result is undefined. The <code>second</code> property is bound to an expression which contains a single, empty expression block (&quot;{}&quot;), which similarly has an undefined result. The <code>third</code> property is bound to an expression which is evaluated as an empty object declaration, and thus the property will be initialized with that empty object value.</p>
<p>Similarly, a colon in JavaScript can be either an object property value assignment, or a code label. Thus, initializing a var property with an object declaration can also require parentheses:</p>
<pre class="qml">

  <span class="type">Item</span> {
      property <span class="type">var</span> <span class="name">first</span>: { <span class="name">example</span>: <span class="string">'true'</span> }    <span class="comment">// example is interpreted as a label</span>
      property <span class="type">var</span> <span class="name">second</span>: ({ example: <span class="string">'true'</span> }) <span class="comment">// example is interpreted as a property</span>
      property <span class="type">var</span> <span class="name">third</span>: { 'example': <span class="string">'true'</span> }  <span class="comment">// example is interpreted as a property</span>
      <span class="name">Component</span>.onCompleted: {
          <span class="name">console</span>.<span class="name">log</span>(<span class="name">first</span>.<span class="name">example</span>) <span class="comment">// prints 'undefined', as &quot;first&quot; was assigned a string</span>
          <span class="name">console</span>.<span class="name">log</span>(<span class="name">second</span>.<span class="name">example</span>) <span class="comment">// prints 'true'</span>
          <span class="name">console</span>.<span class="name">log</span>(<span class="name">third</span>.<span class="name">example</span>) <span class="comment">// prints 'true'</span>
      }
  }

</pre>
</div>
<p><b>See also </b><a href="qtqml-typesystem-basictypes.html">QML Basic Types</a>.</p>
<!-- @@@var -->
        </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>