/usr/share/qt5/doc/qtqml/qtqml-cppintegration-data.html is in qtdeclarative5-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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- data.qdoc -->
<title>Data Type Conversion Between QML and C++ | 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 >Data Type Conversion Between QML and C++</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="#data-ownership">Data Ownership</a></li>
<li class="level1"><a href="#basic-qt-data-types">Basic Qt Data Types</a></li>
<li class="level1"><a href="#qobject-derived-types">QObject-derived Types</a></li>
<li class="level1"><a href="#conversion-between-qt-and-javascript-types">Conversion Between Qt and JavaScript Types</a></li>
<li class="level2"><a href="#qvariantlist-and-qvariantmap-to-javascript-array-and-object">QVariantList and QVariantMap to JavaScript Array and Object</a></li>
<li class="level2"><a href="#qdatetime-to-javascript-date">QDateTime to JavaScript Date</a></li>
<li class="level2"><a href="#qtime-to-javascript-date">QTime to JavaScript Date</a></li>
<li class="level2"><a href="#sequence-type-to-javascript-array">Sequence Type to JavaScript Array</a></li>
<li class="level2"><a href="#value-types">Value Types</a></li>
<li class="level1"><a href="#enumeration-types">Enumeration Types</a></li>
<li class="level2"><a href="#enumeration-types-as-signal-and-method-parameters">Enumeration Types as Signal and Method Parameters</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Data Type Conversion Between QML and C++</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-cppintegration-data.html-description -->
<div class="descr"> <a name="details"></a>
<p>When data values are exchanged between QML and C++, they are converted by the QML engine to have the correct data types as appropriate for use in QML or C++. This requires the exchanged data to be of a type that is recognizable by the engine.</p>
<p>The QML engine provides built-in support for a large number of Qt C++ data types. Additionally, custom C++ types may be registered with the QML type system to make them available to the engine.</p>
<p>This page discusses the data types supported by the QML engine and how they are converted between QML and C++.</p>
<a name="data-ownership"></a>
<h2 id="data-ownership">Data Ownership</h2>
<p>When data is transferred from C++ to QML, the ownership of the data always remains with C++. The exception to this rule is when a <a href="../qtcore/qobject.html">QObject</a> is returned from an explicit C++ method call: in this case, the QML engine assumes ownership of the object, unless the ownership of the object has explicitly been set to remain with C++ by invoking <a href="qqmlengine.html#setObjectOwnership">QQmlEngine::setObjectOwnership</a>() with <a href="qqmlengine.html#ObjectOwnership-enum">QQmlEngine::CppOwnership</a> specified.</p>
<p>Additionally, the QML engine respects the normal <a href="../qtcore/qobject.html">QObject</a> parent ownership semantics of Qt C++ objects, and will never delete a <a href="../qtcore/qobject.html">QObject</a> instance which has a parent.</p>
<a name="basic-qt-data-types"></a>
<h2 id="basic-qt-data-types">Basic Qt Data Types</h2>
<p>By default, QML recognizes the following Qt data types, which are automatically converted to a corresponding <a href="qtqml-typesystem-basictypes.html">QML basic type</a> when passed from C++ to QML and vice-versa:</p>
<div class="table"><table class="generic">
<tr valign="top" class="odd"><td >Qt Type</td><td >QML Basic Type</td></tr>
<tr valign="top" class="even"><td >bool</td><td ><a href="qml-bool.html">bool</a></td></tr>
<tr valign="top" class="odd"><td >unsigned int, int</td><td ><a href="qml-int.html">int</a></td></tr>
<tr valign="top" class="even"><td >double</td><td ><a href="qml-double.html">double</a></td></tr>
<tr valign="top" class="odd"><td >float, qreal</td><td ><a href="qml-real.html">real</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qstring.html">QString</a></td><td ><a href="qml-string.html">string</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qurl.html">QUrl</a></td><td ><a href="qml-url.html">url</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtgui/qcolor.html">QColor</a></td><td ><a href="../qtwidgets/stylesheet-reference.html#color">color</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtgui/qfont.html">QFont</a></td><td ><a href="../qtwidgets/stylesheet-reference.html#font">font</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qdate.html">QDate</a></td><td ><a href="qml-date.html">date</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qpoint.html">QPoint</a>, <a href="../qtcore/qpointf.html">QPointF</a></td><td ><a href="qml-point.html">point</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qsize.html">QSize</a>, <a href="../qtcore/qsizef.html">QSizeF</a></td><td ><a href="qml-size.html">size</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qrect.html">QRect</a>, <a href="../qtcore/qrectf.html">QRectF</a></td><td ><a href="qml-rect.html">rect</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtgui/qmatrix4x4.html">QMatrix4x4</a></td><td >matrix4x4</td></tr>
<tr valign="top" class="odd"><td ><a href="../qtgui/qquaternion.html">QQuaternion</a></td><td >quaternion</td></tr>
<tr valign="top" class="even"><td ><a href="../qtgui/qvector2d.html">QVector2D</a>, <a href="../qtgui/qvector3d.html">QVector3D</a>, <a href="../qtgui/qvector4d.html">QVector4D</a></td><td >vector2d, vector3d, vector4d</td></tr>
<tr valign="top" class="odd"><td >Enums declared with <a href="../qtcore/qobject.html#Q_ENUM">Q_ENUM</a>() or Q_ENUMS()</td><td ><a href="qml-enumeration.html">enumeration</a></td></tr>
</table></div>
<p><b>Note: </b>Classes provided by the <a href="../qtgui/qtgui-index.html">Qt GUI</a> module, such as <a href="../qtgui/qcolor.html">QColor</a>, <a href="../qtgui/qfont.html">QFont</a>, <a href="../qtgui/qquaternion.html">QQuaternion</a> and <a href="../qtgui/qmatrix4x4.html">QMatrix4x4</a>, are only available from QML when the Qt Quick module is included.</p><p>As a convenience, many of these types can be specified in QML by string values, or by a related method provided by the <a href="qml-qtqml-qt.html">QtQml::Qt</a> object. For example, the Image::sourceSize property is of type <a href="qml-size.html">size</a> (which automatically translates to the <a href="../qtcore/qsize.html">QSize</a> type) and can be specified by a string value formatted as "width<code>x</code>height", or by the Qt.size() function:</p>
<pre class="qml">
<span class="type">Item</span> {
<span class="type">Image</span> { <span class="name">sourceSize</span>: <span class="string">"100x200"</span> }
<span class="type">Image</span> { <span class="name">sourceSize</span>: <span class="name">Qt</span>.<span class="name">size</span>(<span class="number">100</span>, <span class="number">200</span>) }
}
</pre>
<p>See documentation for each individual type under <a href="qtqml-typesystem-basictypes.html">QML Basic Types</a> for more information.</p>
<a name="qobject-derived-types"></a>
<h2 id="qobject-derived-types">QObject-derived Types</h2>
<p>Any <a href="../qtcore/qobject.html">QObject</a>-derived class may be used as a type for the exchange of data between QML and C++, providing the class has been registered with the QML type system.</p>
<p>The engine allows the registration of both instantiable and non-instantiable types. Once a class is registered as a QML type, it can be used as a data type for exchanging data between QML and C++. See <a href="qtqml-cppintegration-definetypes.html#registering-c-types-with-the-qml-type-system">Registering C++ types with the QML type system</a> for further details on type registration.</p>
<a name="conversion-between-qt-and-javascript-types"></a>
<h2 id="conversion-between-qt-and-javascript-types">Conversion Between Qt and JavaScript Types</h2>
<p>The QML engine has built-in support for converting a number of Qt types to related JavaScript types, and vice-versa, when transferring data between QML and C++. This makes it possible to use these types and receive them in C++ or JavaScript without needing to implement custom types that provide access to the data values and their attributes.</p>
<p>(Note that the JavaScript environment in QML modifies native JavaScript object prototypes, including those of <code>String</code>, <code>Date</code> and <code>Number</code>, to provide additional features. See the <a href="qtqml-javascript-hostenvironment.html">JavaScript Host Environment</a> for further details.)</p>
<a name="qvariantlist-and-qvariantmap-to-javascript-array-and-object"></a>
<h3 >QVariantList and QVariantMap to JavaScript Array and Object</h3>
<p>The QML engine provides automatic type conversion between <a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a> and JavaScript arrays, and between <a href="../qtcore/qvariant.html#QVariantMap-typedef">QVariantMap</a> and JavaScript objects.</p>
<p>For example, the function defined in QML below expects two arguments, an array and an object, and prints their contents using the standard JavaScript syntax for array and object item access. The C++ code below calls this function, passing a <a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a> and a <a href="../qtcore/qvariant.html#QVariantMap-typedef">QVariantMap</a>, which are automatically converted to JavaScript array and object values, repectively:</p>
<div class="table"><table class="generic">
<tr valign="top" class="odd"><td >QML</td><td ><pre class="qml">
<span class="comment">// MyItem.qml</span>
<span class="type">Item</span> {
<span class="keyword">function</span> <span class="name">readValues</span>(<span class="name">anArray</span>, anObject) {
<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">anArray</span>.<span class="name">length</span>; i++)
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Array item:"</span>, <span class="name">anArray</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">anObject</span>) {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Object item:"</span>, <span class="name">prop</span>, <span class="string">"="</span>, <span class="name">anObject</span>[<span class="name">prop</span>])
}
}
}
</pre>
</td></tr>
<tr valign="top" class="even"><td >C++</td><td ><pre class="cpp">
<span class="comment">// C++</span>
<span class="type">QQuickView</span> view(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">"MyItem.qml"</span>));
<span class="type"><a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a></span> list;
list <span class="operator"><</span><span class="operator"><</span> <span class="number">10</span> <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<span class="type"><a href="qml-qtqml-qt.html">Qt</a></span><span class="operator">::</span>green) <span class="operator"><</span><span class="operator"><</span> <span class="string">"bottles"</span>;
<span class="type"><a href="../qtcore/qvariant.html#QVariantMap-typedef">QVariantMap</a></span> map;
map<span class="operator">.</span>insert(<span class="string">"language"</span><span class="operator">,</span> <span class="string">"QML"</span>);
map<span class="operator">.</span>insert(<span class="string">"released"</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span>(<span class="number">2010</span><span class="operator">,</span> <span class="number">9</span><span class="operator">,</span> <span class="number">21</span>));
<span class="type"><a href="../qtcore/qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>invokeMethod(view<span class="operator">.</span>rootObject()<span class="operator">,</span> <span class="string">"readValues"</span><span class="operator">,</span>
Q_ARG(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>fromValue(list))<span class="operator">,</span>
Q_ARG(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>fromValue(map)));
</pre>
</td></tr>
</table></div>
<p>This produces output like:</p>
<pre class="cpp">
Array item: <span class="number">10</span>
Array item: <span class="preprocessor">#00ff00</span>
Array item: bottles
Object item: language <span class="operator">=</span> QML
Object item: released <span class="operator">=</span> Tue Sep <span class="number">21</span> <span class="number">2010</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> GMT<span class="operator">+</span><span class="number">1000</span> (EST)
</pre>
<p>Similarly, if a C++ type uses a <a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a> or <a href="../qtcore/qvariant.html#QVariantMap-typedef">QVariantMap</a> type for a property type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a <a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a> or <a href="../qtcore/qvariant.html#QVariantMap-typedef">QVariantMap</a> when it is passed to C++.</p>
<a name="qdatetime-to-javascript-date"></a>
<h3 >QDateTime to JavaScript Date</h3>
<p>The QML engine provides automatic type conversion between <a href="../qtcore/qdatetime.html">QDateTime</a> values and JavaScript <code>Date</code> objects.</p>
<p>For example, the function defined in QML below expects a JavaScript <code>Date</code> object, and also returns a new <code>Date</code> object with the current date and time. The C++ code below calls this function, passing a <a href="../qtcore/qdatetime.html">QDateTime</a> value that is automatically converted by the engine into a <code>Date</code> object when it is passed to the <code>readDate()</code> function. In turn, the readDate() function returns a <code>Date</code> object that is automatically converted into a <a href="../qtcore/qdatetime.html">QDateTime</a> value when it is received in C++:</p>
<div class="table"><table class="generic">
<tr valign="top" class="odd"><td >QML</td><td ><pre class="qml">
<span class="comment">// MyItem.qml</span>
<span class="type">Item</span> {
<span class="keyword">function</span> <span class="name">readDate</span>(<span class="name">dt</span>) {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"The given date is:"</span>, <span class="name">dt</span>.<span class="name">toUTCString</span>());
<span class="keyword">return</span> new <span class="name">Date</span>();
}
}
</pre>
</td></tr>
<tr valign="top" class="even"><td >C++</td><td ><pre class="cpp">
<span class="comment">// C++</span>
<span class="type">QQuickView</span> view(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">"MyItem.qml"</span>));
<span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span> dateTime <span class="operator">=</span> <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span><span class="operator">::</span>currentDateTime();
<span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span> retValue;
<span class="type"><a href="../qtcore/qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>invokeMethod(view<span class="operator">.</span>rootObject()<span class="operator">,</span> <span class="string">"readDate"</span><span class="operator">,</span>
Q_RETURN_ARG(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">,</span> retValue)<span class="operator">,</span>
Q_ARG(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>fromValue(dateTime)));
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Value returned from readDate():"</span> <span class="operator"><</span><span class="operator"><</span> retValue;
</pre>
</td></tr>
</table></div>
<p>Similarly, if a C++ type uses a <a href="../qtcore/qdatetime.html">QDateTime</a> for a property type or method parameter, the value can be created as a JavaScript <code>Date</code> object in QML, and is automatically converted to a <a href="../qtcore/qdatetime.html">QDateTime</a> value when it is passed to C++.</p>
<a name="qtime-to-javascript-date"></a>
<h3 >QTime to JavaScript Date</h3>
<p>The QML engine provides automatic type conversion from <a href="../qtcore/qtime.html">QTime</a> values to JavaScript <code>Date</code> objects. The date component of the resulting Date object should not be relied upon, as it is operating system dependent. Specifically, the year (and month and day) are set to zero. Conversion from a JavaScript <code>Date</code> object to <a href="../qtcore/qtime.html">QTime</a> is done by converting to a <a href="../qtcore/qdatetime.html">QDateTime</a>, and then relying on <a href="../qtcore/qvariant.html">QVariant</a> to convert it to a <a href="../qtcore/qtime.html">QTime</a>. The end effect is that the date part of the <code>Date</code> object is ignored, but the local timezone will be used ignoring any DST complications it may have.</p>
<a name="sequence-type-to-javascript-array"></a>
<h3 >Sequence Type to JavaScript Array</h3>
<p>Certain C++ sequence types are supported transparently in QML as JavaScript <code>Array</code> types.</p>
<p>In particular, QML currently supports:</p>
<ul>
<li><code>QList<int></code></li>
<li><code>QList<qreal></code></li>
<li><code>QList<bool></code></li>
<li><code>QList<QString></code> and <code>QStringList</code></li>
<li><code>QList<QUrl></code></li>
<li><code>QVector<int></code></li>
<li><code>QVector<qreal></code></li>
<li><code>QVector<bool></code></li>
</ul>
<p>These sequence types are implemented directly in terms of the underlying C++ sequence. There are two ways in which such sequences can be exposed to QML: as a <a href="../qtcore/qobject.html#Q_PROPERTY">Q_PROPERTY</a> of the given sequence type; or as the return type of a <a href="../qtcore/qobject.html#Q_INVOKABLE">Q_INVOKABLE</a> method. There are some differences in the way these are implemented, which are important to note.</p>
<p>If the sequence is exposed as a <a href="../qtcore/qobject.html#Q_PROPERTY">Q_PROPERTY</a>, accessing any value in the sequence by index will cause the sequence data to be read from the <a href="../qtcore/qobject.html">QObject</a>'s property, then a read to occur. Similarly, modifying any value in the sequence will cause the sequence data to be read, and then the modification will be performed and the modified sequence will be written back to the <a href="../qtcore/qobject.html">QObject</a>'s property.</p>
<p>If the sequence is returned from a <a href="../qtcore/qobject.html#Q_INVOKABLE">Q_INVOKABLE</a> function, access and mutation is much cheaper, as no <a href="../qtcore/qobject.html">QObject</a> property read or write occurs; instead, the C++ sequence data is accessed and modified directly.</p>
<p>Other sequence types are not supported transparently, and instead an instance of any other sequence type will be passed between QML and C++ as an opaque <a href="../qtcore/qvariant.html#QVariantList-typedef">QVariantList</a>.</p>
<p><b>Important Note:</b> There are some minor differences between the semantics of such sequence Array types and default JavaScript Array types which result from the use of a C++ storage type in the implementation. In particular, deleting an element from an Array will result in a default-constructed value replacing that element, rather than an Undefined value. Similarly, setting the length property of the Array to a value larger than its current value will result in the Array being padded out to the specified length with default-constructed elements rather than Undefined elements. Finally, the Qt container classes support signed (rather than unsigned) integer indexes; thus, attempting to access any index greater than INT_MAX will fail.</p>
<p>The default-constructed values for each sequence type are as follows:</p>
<div class="table"><table class="generic">
<tr valign="top" class="odd"><td ><a href="../qtcore/qlist.html">QList</a><int></td><td >integer value 0</td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qlist.html">QList</a><qreal></td><td >real value 0.0</td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qlist.html">QList</a><bool></td><td >boolean value <code>false</code></td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qlist.html">QList</a><<a href="../qtcore/qstring.html">QString</a>> and <a href="../qtcore/qstringlist.html">QStringList</a></td><td >empty <a href="../qtcore/qstring.html">QString</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qlist.html">QList</a><<a href="../qtcore/qurl.html">QUrl</a>></td><td >empty <a href="../qtcore/qurl.html">QUrl</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qvector.html">QVector</a><int></td><td >integer value 0</td></tr>
<tr valign="top" class="odd"><td ><a href="../qtcore/qvector.html">QVector</a><qreal></td><td >real value 0.0</td></tr>
<tr valign="top" class="even"><td ><a href="../qtcore/qvector.html">QVector</a><bool></td><td >boolean value <code>false</code></td></tr>
</table></div>
<p>If you wish to remove elements from a sequence rather than simply replace them with default constructed values, do not use the indexed delete operator ("delete sequence[i]") but instead use the <code>splice</code> function ("sequence.splice(startIndex, deleteCount)").</p>
<a name="value-types"></a>
<h3 >Value Types</h3>
<p>Some value types in Qt such as <a href="../qtcore/qpoint.html">QPoint</a> are represented in JavaScript as objects that have the same properties and functions like in the C++ API. The same representation is possible with custom C++ value types. To enable a custom value type with the QML engine, the class declaration needs to be annotated with <code>Q_GADGET</code>. Properties that are intended to be visible in the JavaScript representation need to be declared with <code>Q_PROPERTY</code>. Similarly functions need to be marked with <code>Q_INVOKABLE</code>. This is the same with <a href="../qtcore/qobject.html">QObject</a> based C++ APIs. For example, the <code>Actor</code> class below is annotated as gadget and has properties:</p>
<pre class="cpp">
<span class="keyword">class</span> Actor
{
Q_GADGET
Q_PROPERTY(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> name READ name WRITE setName)
<span class="keyword">public</span>:
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> name() <span class="keyword">const</span> { <span class="keyword">return</span> m_name; }
<span class="type">void</span> setName(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>name) { m_name <span class="operator">=</span> name; }
<span class="keyword">private</span>:
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> m_name;
};
Q_DECLARE_METATYPE(Actor)
</pre>
<a name="enumeration-types"></a>
<h2 id="enumeration-types">Enumeration Types</h2>
<p>To use a custom enumeration as a data type, its class must be registered and the enumeration must also be declared with <a href="../qtcore/qobject.html#Q_ENUM">Q_ENUM</a>() to register it with Qt's meta object system. For example, the <code>Message</code> class below has a <code>Status</code> enum:</p>
<pre class="cpp">
<span class="keyword">class</span> Message : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
<span class="keyword">public</span>:
<span class="keyword">enum</span> Status {
Ready<span class="operator">,</span>
Loading<span class="operator">,</span>
Error
};
Q_ENUM(Status)
Status status() <span class="keyword">const</span>;
<span class="keyword">signals</span>:
<span class="type">void</span> statusChanged();
};
</pre>
<p>Providing the <code>Message</code> class has been <a href="qtqml-cppintegration-definetypes.html#registering-c-types-with-the-qml-type-system">registered</a> with the QML type system, its <code>Status</code> enum can be used from QML:</p>
<pre class="qml">
<span class="type">Message</span> {
<span class="name">onStatusChanged</span>: {
<span class="keyword">if</span> (<span class="name">status</span> <span class="operator">==</span> <span class="name">Message</span>.<span class="name">Ready</span>)
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Message is loaded!"</span>)
}
}
</pre>
<p>To use an enum as a <a href="../qtcore/qflags.html">flags</a> type in QML, see <a href="../qtcore/qobject.html#Q_FLAG">Q_FLAG</a>().</p>
<p><b>Note: </b>The names of enum values must begin with a capital letter in order to be accessible from QML.</p><a name="enumeration-types-as-signal-and-method-parameters"></a>
<h3 >Enumeration Types as Signal and Method Parameters</h3>
<p>C++ signals and methods with enumeration-type parameters can be used from QML provided that the enumeration and the signal or method are both declared within the same class, or that the enumeration value is one of those declared in the <a href="qml-qtqml-qt.html">Qt Namespace</a>.</p>
<p>Additionally, if a C++ signal with an enum parameter should be connectable to a QML function using the <a href="qtqml-syntax-signals.html#connecting-signals-to-methods-and-signals">connect()</a> function, the enum type must be registered using <a href="../qtcore/qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a>().</p>
<p>For QML signals, enum values may be passed as signal parameters using the <code>int</code> type:</p>
<pre class="qml">
<span class="type">Message</span> {
signal <span class="type">someOtherSignal</span>(int statusValue)
<span class="name">Component</span>.onCompleted: {
<span class="name">someOtherSignal</span>(<span class="name">Message</span>.<span class="name">Loading</span>)
}
}
</pre>
</div>
<!-- @@@qtqml-cppintegration-data.html -->
</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>
|