/usr/share/qt5/doc/qtqml/qml-variant.html is in qtdeclarative5-doc-html 5.5.1-2ubuntu6.
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 | <?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>variant QML Basic Type | Qt QML 5.5</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>Qt 5.5</li>
<li><a href="qtqml-index.html">Qt QML</a></li>
<li><a href="qtqml-qmlmodule.html">QML Types</a></li>
<li>variant QML Basic Type</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</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-scarce-resources-with-the-variant-type">Using Scarce Resources with the variant Type</a></li>
<li class="level1"><a href="#storing-arrays-and-objects">Storing Arrays and Objects</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">variant QML Basic Type</h1>
<span class="subtitle"></span>
<!-- $$$variant-description -->
<div class="descr"> <a name="details"></a>
<p>a generic property type.</p>
<p>The <code>variant</code> type is a generic property type. It is obsolete and exists only to support old applications; new applications should use <a href="qml-var.html">var</a> type properties instead.</p>
<p>A variant type property can hold any of the <a href="qtqml-typesystem-basictypes.html">basic type</a> values:</p>
<pre class="qml"><span class="type">Item</span> {
property <span class="type">variant</span> <span class="name">aNumber</span>: <span class="number">100</span>
property <span class="type">variant</span> <span class="name">aString</span>: <span class="string">"Hello world!"</span>
property <span class="type">variant</span> <span class="name">aBool</span>: <span class="number">false</span>
}</pre>
<p>When integrating with C++, note that any QVariant value <a href="qtqml-cppintegration-data.html">passed into QML from C++</a> is automatically converted into a <code>variant</code> value, and vice-versa.</p>
<a name="using-scarce-resources-with-the-variant-type"></a>
<h2 id="using-scarce-resources-with-the-variant-type">Using Scarce Resources with the variant Type</h2>
<p>A <code>variant</code> type property can also hold an image or pixmap. A <code>variant</code> which contains a QPixmap or QImage is known as a "scarce resource" and the declarative engine will attempt to automatically release such resources after evaluation of any JavaScript expression which requires one to be copied has completed.</p>
<p>Clients may explicitly release such a scarce resource by calling the "destroy" method on the <code>variant</code> property from within JavaScript. They may also explicitly preserve the scarce resource by calling the "preserve" method on the <code>variant</code> property from within JavaScript.</p>
<a name="storing-arrays-and-objects"></a>
<h2 id="storing-arrays-and-objects">Storing Arrays and Objects</h2>
<p>The <code>variant</code> type can also hold:</p>
<ul>
<li>An array of <a href="qtqml-typesystem-basictypes.html">basic type</a> values</li>
<li>A map of key-value pairs with <a href="qtqml-typesystem-basictypes.html">basic-type</a> values</li>
</ul>
<p>For example, below is an <code>items</code> array and an <code>attributes</code> map. Their contents can be examined using JavaScript <code>for</code> loops. Individual array values are accessible by index, and individual map values are accessible by key:</p>
<pre class="qml"><span class="type">Item</span> {
property <span class="type">variant</span> <span class="name">items</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">"four"</span>, <span class="string">"five"</span>]
property <span class="type">variant</span> <span class="name">attributes</span>: { 'color': <span class="string">'red'</span>, 'width': <span class="number">100</span> }
<span class="name">Component</span>.onCompleted: {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">items</span>.<span class="name">length</span>; i++)
<span class="name">console</span>.<span class="name">log</span>(<span class="name">items</span>[<span class="name">i</span>])
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">prop</span> in <span class="name">attributes</span>)
<span class="name">console</span>.<span class="name">log</span>(<span class="name">prop</span>, <span class="string">"="</span>, <span class="name">attributes</span>[<span class="name">prop</span>])
}
}</pre>
<p>While this is a convenient way to store array and map-type values, you must be aware that the <code>items</code> and <code>attributes</code> properties above are <i>not</i> QML objects (and certainly not JavaScript object either) and the key-value pairs in <code>attributes</code> are <i>not</i> QML properties. Rather, the <code>items</code> property holds an array of values, and <code>attributes</code> holds a set of key-value pairs. Since they are stored as a set of values, instead of as an object, their contents <i>cannot</i> be modified individually:</p>
<pre class="qml"><span class="type">Item</span> {
property <span class="type">variant</span> <span class="name">items</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">"four"</span>, <span class="string">"five"</span>]
property <span class="type">variant</span> <span class="name">attributes</span>: { 'color': <span class="string">'red'</span>, 'width': <span class="number">100</span> }
<span class="name">Component</span>.onCompleted: {
<span class="name">items</span>[<span class="number">0</span>] <span class="operator">=</span> <span class="number">10</span>
<span class="name">console</span>.<span class="name">log</span>(<span class="name">items</span>[<span class="number">0</span>]) <span class="comment">// This will still be '1'!</span>
<span class="name">attributes</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">'blue'</span>
<span class="name">console</span>.<span class="name">log</span>(<span class="name">attributes</span>.<span class="name">color</span>) <span class="comment">// This will still be 'red'!</span>
}
}</pre>
<p>Since it is not possible to individually add or remove items from a list or object stored in a <code>variant</code>, the only way to modify its contents is to reassign a new value. However, this is not efficient, as it causes the value to be serialized and deserialized.</p>
<p>Additionally, since <code>items</code> and <code>attributes</code> are not QML objects, changing their individual values do not trigger property change notifications. If the above example had <code>onNumberChanged</code> or <code>onAnimalChanged</code> signal handlers, they would not have been called. If, however, the <code>items</code> or <code>attributes</code> properties themselves were reassigned to different values, then such handlers would be called.</p>
<p>JavaScript programmers should also note that when a JavaScript object is copied to an array or map property, the <i>contents</i> of the object (that is, its key-value properties) are copied, rather than the object itself. The property does not hold a reference to the original JavaScript object, and extra data such as the object's JavaScript prototype chain is also lost in the process.</p>
<p>This basic type is provided by the QML language.</p>
</div>
<p><b>See also </b><a href="qtqml-typesystem-basictypes.html">QML Basic Types</a>.</p>
<!-- @@@variant -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 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>
|