/usr/share/qt5/doc/qtqml/qtqml-syntax-objectattributes.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 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- objectattributes.qdoc -->
<title>QML Object Attributes | 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>QML Object Attributes</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="#attributes-in-object-declarations">Attributes in Object Declarations</a></li>
<li class="level2"><a href="#the-id-attribute">The <i>id</i> Attribute</a></li>
<li class="level2"><a href="#property-attributes">Property Attributes</a></li>
<li class="level2"><a href="#signal-attributes">Signal Attributes</a></li>
<li class="level2"><a href="#signal-handler-attributes">Signal Handler Attributes</a></li>
<li class="level2"><a href="#method-attributes">Method Attributes</a></li>
<li class="level2"><a href="#attached-properties-and-attached-signal-handlers">Attached Properties and Attached Signal Handlers</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML Object Attributes</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-syntax-objectattributes.html-description -->
<div class="descr"> <a name="details"></a>
<p>Every QML object type has a defined set of attributes. Each instance of an object type is created with the set of attributes that have been defined for that object type. There are several different kinds of attributes which can be specified, which are described below.</p>
<a name="attributes-in-object-declarations"></a>
<h2 id="attributes-in-object-declarations">Attributes in Object Declarations</h2>
<p>An <a href="qtqml-syntax-basics.html#object-declarations">object declaration</a> in a QML document defines a new type. It also declares an object hierarchy that will be instantiated should an instance of that newly defined type be created.</p>
<p>The set of QML object-type attribute types is as follows:</p>
<ul>
<li>the <i>id</i> attribute</li>
<li>property attributes</li>
<li>signal attributes</li>
<li>signal handler attributes</li>
<li>method attributes</li>
<li>attached properties and attached signal handler attributes</li>
</ul>
<p>These attributes are discussed in detail below.</p>
<a name="the-id-attribute"></a>
<h3 >The <i>id</i> Attribute</h3>
<p>Every QML object type has exactly one <i>id</i> attribute. This attribute is provided by the language itself, and cannot be redefined or overridden by any QML object type.</p>
<p>A value may be assigned to the <i>id</i> attribute of an object instance to allow that object to be identified and referred to by other objects. This <code>id</code> must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.</p>
<p>Below is a TextInput object and a Text object. The TextInput object's <code>id</code> value is set to "myTextInput". The Text object sets its <code>text</code> property to have the same value as the <code>text</code> property of the TextInput, by referring to <code>myTextInput.text</code>. Now, both items will display the same text:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Column</span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">TextInput</span> { <span class="name">id</span>: <span class="name">myTextInput</span>; <span class="name">text</span>: <span class="string">"Hello World"</span> }
<span class="type">Text</span> { <span class="name">text</span>: <span class="name">myTextInput</span>.<span class="name">text</span> }
}</pre>
<p>An object can be referred to by its <code>id</code> from anywhere within the <i>component scope</i> in which it is declared. Therefore, an <code>id</code> value must always be unique within its component scope. See <a href="qtqml-documents-scope.html">Scope and Naming Resolution</a> for more information.</p>
<p>Once an object instance is created, the value of its <i>id</i> attribute cannot be changed. While it may look like an ordinary property, the <code>id</code> attribute is <b>not</b> an ordinary <code>property</code> attribute, and special semantics apply to it; for example, it is not possible to access <code>myTextInput.id</code> in the above example.</p>
<a name="property-attributes"></a>
<h3 >Property Attributes</h3>
<p>A property is an attribute of an object that can be assigned a static value or bound to a dynamic expression. A property's value can be read by other objects. Generally it can also be modified by another object, unless a particular QML type has explicitly disallowed this for a specific property.</p>
<a name="defining-property-attributes"></a>
<h4 >Defining Property Attributes</h4>
<p>A property may be defined for a type in C++ by registering a Q_PROPERTY of a class which is then registered with the QML type system. Alternatively, a custom property of an object type may be defined in an object declaration in a QML document with the following syntax:</p>
<pre class="cpp"> <span class="operator">[</span><span class="keyword">default</span><span class="operator">]</span> property <span class="operator"><</span>propertyType<span class="operator">></span> <span class="operator"><</span>propertyName<span class="operator">></span></pre>
<p>In this way an object declaration may <a href="qtqml-typesystem-objecttypes.html#defining-object-types-from-qml">expose a particular value</a> to outside objects or maintain some internal state more easily.</p>
<p>Property names must begin with a lower case letter and can only contain letters, numbers and underscores. JavaScript reserved words are not valid property names. The <code>default</code> keyword is optional, and modifies the semantics of the property being declared. See the upcoming section on <a href="qtqml-syntax-objectattributes.html#default-properties">default properties</a> for more information about the <code>default</code> property modifier.</p>
<p>Declaring a custom property implicitly creates a value-change <a href="qtqml-syntax-objectattributes.html#signal-attributes">signal</a> for that property, as well as an associated <a href="qtqml-syntax-objectattributes.html#signal-handler-attributes">signal handler</a> called <i>on<PropertyName>Changed</i>, where <i><PropertyName></i> is the name of the property, with the first letter capitalized.</p>
<p>For example, the following object declaration defines a new type which derives from the Rectangle base type. It has two new properties, with a <a href="qtqml-syntax-objectattributes.html#signal-handler-attributes">signal handler</a> implemented for one of those new properties:</p>
<pre class="qml"><span class="type">Rectangle</span> {
property <span class="type">color</span> <span class="name">previousColor</span>
property <span class="type">color</span> <span class="name">nextColor</span>
<span class="name">onNextColorChanged</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">"The next color will be: "</span> <span class="operator">+</span> <span class="name">nextColor</span>.<span class="name">toString</span>())
}</pre>
<a name="valid-types-in-custom-property-definitions"></a>
<h5 >Valid Types in Custom Property Definitions</h5>
<p>Any of the <a href="qtqml-typesystem-basictypes.html">QML Basic Types</a> aside from the <a href="qml-enumeration.html">enumeration</a> type can be used as custom property types. For example, these are all valid property declarations:</p>
<pre class="qml"><span class="type">Item</span> {
property <span class="type">int</span> <span class="name">someNumber</span>
property <span class="type">string</span> <span class="name">someString</span>
property <span class="type">url</span> <span class="name">someUrl</span>
}</pre>
<p>(Enumeration values are simply whole number values and can be referred to with the <a href="qml-int.html">int</a> type instead.)</p>
<p>Some basic types are provided by the <code>QtQuick</code> module and thus cannot be used as property types unless the module is imported. See the <a href="qtqml-typesystem-basictypes.html">QML Basic Types</a> documentation for more details.</p>
<p>Note the <a href="qml-var.html">var</a> basic type is a generic placeholder type that can hold any type of value, including lists and objects:</p>
<pre class="cpp">property var someNumber: <span class="number">1.5</span>
property var someString: <span class="string">"abc"</span>
property var someBool: <span class="keyword">true</span>
property var someList: <span class="operator">[</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="string">"three"</span><span class="operator">,</span> <span class="string">"four"</span><span class="operator">]</span>
property var someObject: Rectangle { width: <span class="number">100</span>; height: <span class="number">100</span>; color: <span class="string">"red"</span> }</pre>
<p>Additionally, any <a href="qtqml-typesystem-objecttypes.html">QML object type</a> can be used as a property type. For example:</p>
<pre class="cpp">property Item someItem
property Rectangle someRectangle</pre>
<p>This applies to <a href="qtqml-typesystem-objecttypes.html#defining-object-types-from-qml">custom QML types</a> as well. If a QML type was defined in a file named <code>ColorfulButton.qml</code> (in a directory which was then imported by the client), then a property of type <code>ColorfulButton</code> would also be valid.</p>
<a name="assigning-values-to-property-attributes"></a>
<h4 >Assigning Values to Property Attributes</h4>
<p>The value of a property of an object instance may specified in two separate ways:</p>
<ul>
<li>a value assignment on initialization</li>
<li>an imperative value assignment</li>
</ul>
<p>In either case, the value may be either a <i>static</i> value or a <i>binding expression</i> value.</p>
<a name="value-assignment-on-initialization"></a>
<h5 >Value Assignment on Initialization</h5>
<p>The syntax for assigning a value to a property on initialization is:</p>
<pre class="cpp"> <span class="operator"><</span>propertyName<span class="operator">></span> : <span class="operator"><</span>value<span class="operator">></span></pre>
<p>An initialization value assignment may be combined with a property definition in an object declaration, if desired. In that case, the syntax of the property definition becomes:</p>
<pre class="cpp"> <span class="operator">[</span><span class="keyword">default</span><span class="operator">]</span> property <span class="operator"><</span>propertyType<span class="operator">></span> <span class="operator"><</span>propertyName<span class="operator">></span> : <span class="operator"><</span>value<span class="operator">></span></pre>
<p>An example of property value initialization follows:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Rectangle</span> {
<span class="name">color</span>: <span class="string">"red"</span>
property <span class="type">color</span> <span class="name">nextColor</span>: <span class="string">"blue"</span> <span class="comment">// combined property declaration and initialization</span>
}</pre>
<a name="imperative-value-assignment"></a>
<h5 >Imperative Value Assignment</h5>
<p>An imperative value assignment is where a property value (either static value or binding expression) is assigned to a property from imperative JavaScript code. The syntax of an imperative value assignment is just the JavaScript assignment operator, as shown below:</p>
<pre class="cpp"> <span class="operator">[</span><span class="operator"><</span>objectId<span class="operator">></span><span class="operator">.</span><span class="operator">]</span><span class="operator"><</span>propertyName<span class="operator">></span> <span class="operator">=</span> value</pre>
<p>An example of imperative value assignment follows:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Rectangle</span> {
<span class="name">id</span>: <span class="name">rect</span>
<span class="name">Component</span>.onCompleted: {
<span class="name">rect</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">"red"</span>
}
}</pre>
<a name="static-values-and-binding-expression-values"></a>
<h4 >Static Values and Binding Expression Values</h4>
<p>As previously noted, there are two kinds of values which may be assigned to a property: <i>static</i> values, and <i>binding expression</i> values. The latter are also known as <a href="qtqml-syntax-propertybinding.html">property bindings</a>.</p>
<div class="table"><table class="generic">
<thead><tr class="qt-style"><th >Kind</th><th >Semantics</th></tr></thead>
<tr valign="top" class="odd"><td >Static Value</td><td >A constant value which does not depend on other properties.</td></tr>
<tr valign="top" class="even"><td >Binding Expression</td><td >A JavaScript expression which describes a property's relationship with other properties. The variables in this expression are called the property's <i>dependencies</i>.<p>The QML engine enforces the relationship between a property and its dependencies. When any of the dependencies change in value, the QML engine automatically re-evaluates the binding expression and assigns the new result to the property.</p>
</td></tr>
</table></div>
<p>Here is an example that shows both kinds of values being assigned to properties:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Rectangle</span> {
<span class="comment">// both of these are static value assignments on initialization</span>
<span class="name">width</span>: <span class="number">400</span>
<span class="name">height</span>: <span class="number">200</span>
<span class="type">Rectangle</span> {
<span class="comment">// both of these are binding expression value assignments on initialization</span>
<span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
<span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span>
}
}</pre>
<p><b>Note: </b>To assign a binding expression imperatively, the binding expression must be contained in a function that is passed into <a href="qml-qtqml-qt.html#binding-method">Qt.binding()</a>, and then the value returned by Qt.binding() must be assigned to the property. In contrast, Qt.binding() must not be used when assigning a binding expression upon initialization. See <a href="qtqml-syntax-propertybinding.html">Property Binding</a> for more information.</p><a name="type-safety"></a>
<h4 >Type Safety</h4>
<p>Properties are type safe. A property can only be assigned a value that matches the property type.</p>
<p>For example, if a property is a real, and if you try to assign a string to it, you will get an error:</p>
<pre class="cpp">property <span class="type">int</span> volume: <span class="string">"four"</span> <span class="comment">// generates an error; the property's object will not be loaded</span></pre>
<p>Likewise if a property is assigned a value of the wrong type during run time, the new value will not be assigned, and an error will be generated.</p>
<p>Some property types do not have a natural value representation, and for those property types the QML engine automatically performs string-to-typed-value conversion. So, for example, even though properties of the <code>color</code> type store colors and not strings, you are able to assign the string <code>"red"</code> to a color property, without an error being reported.</p>
<p>See <a href="qtqml-typesystem-basictypes.html">QML Basic Types</a> for a list of the types of properties that are supported by default. Additionally, any available <a href="qtqml-typesystem-objecttypes.html">QML object type</a> may also be used as a property type.</p>
<a name="special-property-types"></a>
<h4 >Special Property Types</h4>
<a name="object-list-property-attributes"></a>
<h5 >Object List Property Attributes</h5>
<p>A <a href="qml-list.html">list</a> type property can be assigned a list of QML object-type values. The syntax for defining an object list value is a comma-separated list surrounded by square brackets:</p>
<pre class="cpp"> <span class="operator">[</span> <span class="operator"><</span>item <span class="number">1</span><span class="operator">></span><span class="operator">,</span> <span class="operator"><</span>item <span class="number">2</span><span class="operator">></span><span class="operator">,</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="operator">]</span></pre>
<p>For example, the Item type has a states property that is used to hold a list of <a href="qml-qtqml-statemachine-state.html">State</a> type objects. The code below initializes the value of this property to a list of three <a href="qml-qtqml-statemachine-state.html">State</a> objects:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Item</span> {
<span class="name">states</span>: [
<span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"loading"</span> },
<span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"running"</span> },
<span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"stopped"</span> }
]
}</pre>
<p>If the list contains a single item, the square brackets may be omitted:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Item</span> {
<span class="name">states</span>: <span class="name">State</span> { <span class="name">name</span>: <span class="string">"running"</span> }
}</pre>
<p>A <a href="qml-list.html">list</a> type property may be specified in an object declaration with the following syntax:</p>
<pre class="cpp"> <span class="operator">[</span><span class="keyword">default</span><span class="operator">]</span> property list<span class="operator"><</span><span class="operator"><</span>objectType<span class="operator">></span><span class="operator">></span> propertyName</pre>
<p>and, like other property declarations, a property initialization may be combined with the property declaration with the following syntax:</p>
<pre class="cpp"> <span class="operator">[</span><span class="keyword">default</span><span class="operator">]</span> property list<span class="operator"><</span><span class="operator"><</span>objectType<span class="operator">></span><span class="operator">></span> propertyName: <span class="operator"><</span>value<span class="operator">></span></pre>
<p>An example of list property declaration follows:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Rectangle</span> {
<span class="comment">// declaration without initialization</span>
property list<<span class="type">Rectangle</span>> <span class="name">siblingRects</span>
<span class="comment">// declaration with initialization</span>
property list<<span class="type">Rectangle</span>> <span class="name">childRects</span>: [
<span class="type">Rectangle</span> { <span class="name">color</span>: <span class="string">"red"</span> },
<span class="type">Rectangle</span> { <span class="name">color</span>: <span class="string">"blue"</span>}
]
}</pre>
<p>If you wish to declare a property to store a list of values which are not necessarily QML object-type values, you should declare a <a href="qml-var.html">var</a> property instead.</p>
<a name="grouped-properties"></a>
<h5 >Grouped Properties</h5>
<p>In some cases properties contain a logical group of sub-property attributes. These sub-property attributes can be assigned to using either the dot notation or group notation.</p>
<p>For example, the Text type has a font group property. Below, the first Text object initializes its <code>font</code> values using dot notation, while the second uses group notation:</p>
<pre class="cpp">Text {
<span class="comment">//dot notation</span>
font<span class="operator">.</span>pixelSize: <span class="number">12</span>
font<span class="operator">.</span>b: <span class="keyword">true</span>
}
Text {
<span class="comment">//group notation</span>
font { pixelSize: <span class="number">12</span>; b: <span class="keyword">true</span> }
}</pre>
<p>Grouped property types are basic types which have subproperties. Some of these basic types are provided by the QML language, while others may only be used if the Qt Quick module is imported. See the documentation about <a href="qtqml-typesystem-basictypes.html">QML Basic Types</a> for more information.</p>
<a name="property-aliases"></a>
<h4 >Property Aliases</h4>
<p>Property aliases are properties which hold a reference to another property. Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).</p>
<p>A property alias declaration looks like an ordinary property definition, except that it requires the <code>alias</code> keyword instead of a property type, and the right-hand-side of the property declaration must be a valid alias reference:</p>
<pre class="cpp"><span class="operator">[</span><span class="keyword">default</span><span class="operator">]</span> property alias <span class="operator"><</span>name<span class="operator">></span>: <span class="operator"><</span>alias reference<span class="operator">></span></pre>
<p>Unlike an ordinary property, an alias can only refer to a object, or the property of a object, that is within the scope of the <a href="qtqml-typesystem-objecttypes.html">type</a> within which the alias is declared. It cannot contain arbitrary JavaScript expressions and it cannot refer to objects declared outside of the scope of its type. Also note the <i>alias reference</i> is not optional, unlike the optional default value for an ordinary property; the alias reference must be provided when the alias is first declared.</p>
<p>For example, below is a <code>Button</code> type with a <code>buttonText</code> aliased property which is connected to the <code>text</code> object of the Text child:</p>
<pre class="qml"><span class="comment">// Button.qml</span>
import QtQuick 2.0
<span class="type">Rectangle</span> {
property <span class="type">alias</span> <span class="name">buttonText</span>: <span class="name">textItem</span>.<span class="name">text</span>
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>; <span class="name">color</span>: <span class="string">"yellow"</span>
<span class="type">Text</span> { <span class="name">id</span>: <span class="name">textItem</span> }
}</pre>
<p>The following code would create a <code>Button</code> with a defined text string for the child Text object:</p>
<pre class="qml"><span class="type">Button</span> { <span class="name">buttonText</span>: <span class="string">"Click Me"</span> }</pre>
<p>Here, modifying <code>buttonText</code> directly modifies the textItem.text value; it does not change some other value that then updates textItem.text. If <code>buttonText</code> was not an alias, changing its value would not actually change the displayed text at all, as property bindings are not bi-directional: the <code>buttonText</code> value would have changed if textItem.text was changed, but not the other way around.</p>
<a name="considerations-for-property-aliases"></a>
<h5 >Considerations for Property Aliases</h5>
<p>Aliases are only activated once a component has been fully initialized. An error is generated when an uninitialized alias is referenced. Likewise, aliasing an aliasing property will also result in an error.</p>
<pre class="qml">property <span class="type">alias</span> <span class="name">widgetLabel</span>: <span class="name">label</span>
<span class="comment">//will generate an error</span>
<span class="comment">//widgetLabel.text: "Initial text"</span>
<span class="comment">//will generate an error</span>
<span class="comment">//property alias widgetLabelText: widgetLabel.text</span>
<span class="name">Component</span>.onCompleted: <span class="name">widgetLabel</span>.<span class="name">text</span> <span class="operator">=</span> <span class="string">"Alias completed Initialization"</span></pre>
<p>When importing a <a href="qtqml-typesystem-objecttypes.html">QML object type</a> with a property alias in the root object, however, the property appear as a regular Qt property and consequently can be used in alias references.</p>
<p>It is possible for an aliasing property to have the same name as an existing property, effectively overwriting the existing property. For example, the following QML type has a <code>color</code> alias property, named the same as the built-in Rectangle::color property:</p>
<pre class="qml"><span class="type">Rectangle</span> {
<span class="name">id</span>: <span class="name">coloredrectangle</span>
property <span class="type">alias</span> <span class="name">color</span>: <span class="name">bluerectangle</span>.<span class="name">color</span>
<span class="name">color</span>: <span class="string">"red"</span>
<span class="type">Rectangle</span> {
<span class="name">id</span>: <span class="name">bluerectangle</span>
<span class="name">color</span>: <span class="string">"#1234ff"</span>
}
<span class="name">Component</span>.onCompleted: {
<span class="name">console</span>.<span class="name">log</span> (<span class="name">coloredrectangle</span>.<span class="name">color</span>) <span class="comment">//prints "#1234ff"</span>
<span class="name">setInternalColor</span>()
<span class="name">console</span>.<span class="name">log</span> (<span class="name">coloredrectangle</span>.<span class="name">color</span>) <span class="comment">//prints "#111111"</span>
<span class="name">coloredrectangle</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">"#884646"</span>
<span class="name">console</span>.<span class="name">log</span> (<span class="name">coloredrectangle</span>.<span class="name">color</span>) <span class="comment">//prints #884646</span>
}
<span class="comment">//internal function that has access to internal properties</span>
<span class="keyword">function</span> <span class="name">setInternalColor</span>() {
<span class="name">color</span> <span class="operator">=</span> <span class="string">"#111111"</span>
}
}</pre>
<p>Any object that use this type and refer to its <code>color</code> property will be referring to the alias rather than the ordinary Rectangle::color property. Internally, however, the red can correctly set its <code>color</code> property and refer to the actual defined property rather than the alias.</p>
<a name="default-properties"></a>
<h4 >Default Properties</h4>
<p>An object definition can have a single <i>default</i> property. A default property is the property to which a value is assigned if an object is declared within another object's definition without declaring it as a value for a particular property.</p>
<p>Declaring a property with the optional <code>default</code> keyword marks it as the default property. For example, say there is a file MyLabel.qml with a default property <code>someText</code>:</p>
<pre class="qml"><span class="comment">// MyLabel.qml</span>
import QtQuick 2.0
<span class="type">Text</span> {
default property <span class="type">var</span> <span class="name">someText</span>
<span class="name">text</span>: <span class="string">"Hello, "</span> <span class="operator">+</span> <span class="name">someText</span>.<span class="name">text</span>
}</pre>
<p>The <code>someText</code> value could be assigned to in a <code>MyLabel</code> object definition, like this:</p>
<pre class="qml"><span class="type">MyLabel</span> {
<span class="type">Text</span> { <span class="name">text</span>: <span class="string">"world!"</span> }
}</pre>
<p>This has exactly the same effect as the following:</p>
<pre class="qml"><span class="type">MyLabel</span> {
<span class="name">someText</span>: <span class="name">Text</span> { <span class="name">text</span>: <span class="string">"world!"</span> }
}</pre>
<p>However, since the <code>someText</code> property has been marked as the default property, it is not necessary to explicitly assign the Text object to this property.</p>
<p>You will notice that child objects can be added to any Item-based type without explicitly adding them to the children property. This is because the default property of Item is its <code>data</code> property, and any items added to this list for an Item are automatically added to its list of children.</p>
<p>Default properties can be useful for reassigning the children of an item. See the TabWidget Example, which uses a default property to automatically reassign children of the TabWidget as children of an inner ListView.</p>
<a name="read-only-properties"></a>
<h4 >Read-Only Properties</h4>
<p>An object declaration may define a read-only property using the <code>readonly</code> keyword, with the following syntax:</p>
<pre class="cpp"> readonly property <span class="operator"><</span>propertyType<span class="operator">></span> <span class="operator"><</span>propertyName<span class="operator">></span> : <span class="operator"><</span>initialValue<span class="operator">></span></pre>
<p>Read-only properties must be assigned a value on initialization. After a read-only property is initialized, it no longer possible to give it a value, whether from imperative code or otherwise.</p>
<p>For example, the code in the <code>Component.onCompleted</code> block below is invalid:</p>
<pre class="qml"><span class="type">Item</span> {
readonly property <span class="type">int</span> <span class="name">someNumber</span>: <span class="number">10</span>
<span class="name">Component</span>.onCompleted: <span class="name">someNumber</span> <span class="operator">=</span> <span class="number">20</span> <span class="comment">// doesn't work, causes an error</span>
}</pre>
<p><b>Note: </b>A read-only property cannot also be a <a href="qtqml-syntax-objectattributes.html#default-properties">default</a> property.</p><a name="property-modifier-objects"></a>
<h4 >Property Modifier Objects</h4>
<p>Properties can have <a href="qtqml-cppintegration-definetypes.html#property-modifier-types">property value modifier objects</a> associated with them. The syntax for declaring an instance of a property modifier type associated with a particular property is as follows:</p>
<pre class="cpp"><span class="operator"><</span>PropertyModifierTypeName<span class="operator">></span> on <span class="operator"><</span>propertyName<span class="operator">></span> {
<span class="comment">// attributes of the object instance</span>
}</pre>
<p>It is important to note that the above syntax is in fact an <a href="qtqml-syntax-basics.html#object-declarations">object declaration</a> which will instantiate an object which acts on a pre-existing property.</p>
<p>Certain property modifier types may only be applicable to specific property types, however this is not enforced by the language. For example, the <code>NumberAnimation</code> type provided by <code>QtQuick</code> will only animate numeric-type (such as <code>int</code> or <code>real</code>) properties. Attempting to use a <code>NumberAnimation</code> with non-numeric property will not result in an error, however the non-numeric property will not be animated. The behavior of a property modifier type when associated with a particular property type is defined by its implementation.</p>
<a name="signal-attributes"></a>
<h3 >Signal Attributes</h3>
<p>A signal is a notification from an object that some event has occurred: for example, a property has changed, an animation has started or stopped, or when an image has been downloaded. The MouseArea type, for example, has a clicked signal that is emitted when the user clicks within the mouse area.</p>
<p>An object can be notified through a <a href="qtqml-syntax-objectattributes.html#signal-handler-attributes">signal handler</a> whenever it a particular signal is emitted. A signal handler is declared with the syntax <i>on<Signal></i> where <i><Signal></i> is the name of the signal, with the first letter capitalized. The signal handler must be declared within the definition of the object that emits the signal, and the handler should contain the block of JavaScript code to be executed when the signal handler is invoked.</p>
<p>For example, the <i>onClicked</i> signal handler below is declared within the MouseArea object definition, and is invoked when the MouseArea is clicked, causing a console message to be printed:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Item</span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="type">MouseArea</span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Click!"</span>)
}
}
}</pre>
<a name="defining-signal-attributes"></a>
<h4 >Defining Signal Attributes</h4>
<p>A signal may be defined for a type in C++ by registering a Q_SIGNAL of a class which is then registered with the QML type system. Alternatively, a custom signal for an object type may be defined in an object declaration in a QML document with the following syntax:</p>
<pre class="cpp"> signal <span class="operator"><</span>signalName<span class="operator">></span><span class="operator">[</span>(<span class="operator">[</span><span class="operator"><</span>type<span class="operator">></span> <span class="operator"><</span>parameter name<span class="operator">></span><span class="operator">[</span><span class="operator">,</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span><span class="operator">]</span><span class="operator">]</span>)<span class="operator">]</span></pre>
<p>Attempting to declare two signals or methods with the same name in the same type block is an error. However, a new signal may reuse the name of an existing signal on the type. (This should be done with caution, as the existing signal may be hidden and become inaccessible.)</p>
<p>Here are three examples of signal declarations:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Item</span> {
signal <span class="type">clicked</span>
signal <span class="type">hovered</span>()
signal <span class="type">actionPerformed</span>(string action, var actionResult)
}</pre>
<p>If the signal has no parameters, the "()" brackets are optional. If parameters are used, the parameter types must be declared, as for the <code>string</code> and <code>var</code> arguments for the <code>actionPerformed</code> signal above. The allowed parameter types are the same as those listed under <a href="qtqml-syntax-objectattributes.html#defining-property-attributes">Defining Property Attributes</a> on this page.</p>
<p>To emit a signal, invoke it as a method. Any relevant <a href="qtqml-syntax-objectattributes.html#signal-handler-attributes">signal handlers</a> will be invoked when the signal is emitted, and handlers can use the defined signal argument names to access the respective arguments.</p>
<a name="property-change-signals"></a>
<h4 >Property Change Signals</h4>
<p>QML types also provide built-in <i>property change signals</i> that are emitted whenever a property value changes, as previously described in the section on <a href="qtqml-syntax-objectattributes.html#property-attributes">property attributes</a>. See the upcoming section on <a href="qtqml-syntax-signals.html#property-change-signal-handlers">property change signal handlers</a> for more information about why these signals are useful, and how to use them.</p>
<a name="signal-handler-attributes"></a>
<h3 >Signal Handler Attributes</h3>
<p>Signal handlers are a special sort of <a href="qtqml-syntax-objectattributes.html#method-attributes">method attribute</a>, where the method implementation is invoked by the QML engine whenever the associated signal is emitted. Adding a signal to an object definition in QML will automatically add an associated signal handler to the object definition, which has, by default, an empty implementation. Clients can provide an implementation, to implement program logic.</p>
<p>Consider the following <code>SquareButton</code> type, whose definition is provided in the <code>SquareButton.qml</code> file as shown below, with signals <code>activated</code> and <code>deactivated</code>:</p>
<pre class="qml"><span class="comment">// SquareButton.qml</span>
<span class="type">Rectangle</span> {
<span class="name">id</span>: <span class="name">root</span>
signal <span class="type">activated</span>(real xPosition, real yPosition)
signal <span class="type">deactivated</span>
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="type">MouseArea</span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onPressed</span>: <span class="name">root</span>.<span class="name">activated</span>(<span class="name">mouse</span>.<span class="name">x</span>, <span class="name">mouse</span>.<span class="name">y</span>)
<span class="name">onRelased</span>: <span class="name">root</span>.<span class="name">deactivated</span>()
}
}</pre>
<p>These signals could be received by any <code>SquareButton</code> objects in another QML file in the same directory, where implementations for the signal handlers are provided by the client:</p>
<pre class="qml"><span class="comment">// myapplication.qml</span>
<span class="type">SquareButton</span> {
<span class="name">onActivated</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">"Activated at "</span> <span class="operator">+</span> <span class="name">xPosition</span> <span class="operator">+</span> <span class="string">","</span> <span class="operator">+</span> <span class="name">yPosition</span>)
<span class="name">onDeactivated</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">"Deactivated!"</span>)
}</pre>
<p>See the <a href="qtqml-syntax-signals.html">Signal and Handler Event System</a> for more details on use of signals.</p>
<a name="property-change-signal-handlers"></a>
<h4 >Property Change Signal Handlers</h4>
<p>Signal handlers for property change signal take the syntax form <i>on<Property>Changed</i> where <i><Property></i> is the name of the property, with the first letter capitalized. For example, although the TextInput type documentation does not document a <code>textChanged</code> signal, this signal is implicitly available through the fact that TextInput has a text property and so it is possible to write an <code>onTextChanged</code> signal handler to be called whenever this property changes:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">TextInput</span> {
<span class="name">text</span>: <span class="string">"Change this!"</span>
<span class="name">onTextChanged</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">"Text has changed to:"</span>, <span class="name">text</span>)
}</pre>
<a name="method-attributes"></a>
<h3 >Method Attributes</h3>
<p>A method of an object type is a function which may be called to perform some processing or trigger further events. A method can be connected to a signal so that it is automatically invoked whenever the signal is emitted. See <a href="qtqml-syntax-signals.html">Signal and Handler Event System</a> for more details.</p>
<a name="defining-method-attributes"></a>
<h4 >Defining Method Attributes</h4>
<p>A method may be defined for a type in C++ by tagging a function of a class which is then registered with the QML type system with Q_INVOKABLE or by registering it as a Q_SLOT of the class. Alternatively, a custom method can be added to an object declaration in a QML document with the following syntax:</p>
<pre class="cpp"> function <span class="operator"><</span>functionName<span class="operator">></span>(<span class="operator">[</span><span class="operator"><</span>parameterName<span class="operator">></span><span class="operator">[</span><span class="operator">,</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span><span class="operator">]</span><span class="operator">]</span>) { <span class="operator"><</span>body<span class="operator">></span> }</pre>
<p>Methods can be added to a QML type in order to define standalone, reusable blocks of JavaScript code. These methods can be invoked either internally or by external objects.</p>
<p>Unlike signals, method parameter types do not have to be declared as they default to the <code>var</code> type.</p>
<p>Attempting to declare two methods or signals with the same name in the same type block is an error. However, a new method may reuse the name of an existing method on the type. (This should be done with caution, as the existing method may be hidden and become inaccessible.)</p>
<p>Below is a Rectangle with a <code>calculateHeight()</code> method that is called when assigning the <code>height</code> value:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Rectangle</span> {
<span class="name">id</span>: <span class="name">rect</span>
<span class="keyword">function</span> <span class="name">calculateHeight</span>() {
<span class="keyword">return</span> <span class="name">rect</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>;
}
<span class="name">width</span>: <span class="number">100</span>
<span class="name">height</span>: <span class="name">calculateHeight</span>()
}</pre>
<p>If the method has parameters, they are accessible by name within the method. Below, when the MouseArea is clicked it invokes the <code>moveTo()</code> method which can then refer to the received <code>newX</code> and <code>newY</code> parameters to reposition the text:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">Item</span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">MouseArea</span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: <span class="name">label</span>.<span class="name">moveTo</span>(<span class="name">mouse</span>.<span class="name">x</span>, <span class="name">mouse</span>.<span class="name">y</span>)
}
<span class="type">Text</span> {
<span class="name">id</span>: <span class="name">label</span>
<span class="keyword">function</span> <span class="name">moveTo</span>(<span class="name">newX</span>, newY) {
<span class="name">label</span>.<span class="name">x</span> <span class="operator">=</span> <span class="name">newX</span>;
<span class="name">label</span>.<span class="name">y</span> <span class="operator">=</span> <span class="name">newY</span>;
}
<span class="name">text</span>: <span class="string">"Move me!"</span>
}
}</pre>
<a name="attached-properties-and-attached-signal-handlers"></a>
<h3 >Attached Properties and Attached Signal Handlers</h3>
<p><i>Attached properties</i> and <i>attached signal handlers</i> are mechanisms that enable objects to be annotated with extra properties or signal handlers that are otherwise unavailable to the object. In particular, they allow objects to access properties or signals that are specifically relevant to the individual object.</p>
<p>A QML type implementation may choose to create an <i>attaching type</i> with particular properties and signals. Instances of this type can then be created and <i>attached</i> to specific objects at run time, allowing those objects to access the properties and signals of the attaching type. These are accessed by prefixing the properties and respective signal handlers with the name of the attaching type.</p>
<p>References to attached properties and handlers take the following syntax form:</p>
<pre class="cpp"><span class="operator"><</span>AttachingType<span class="operator">></span><span class="operator">.</span><span class="operator"><</span>propertyName<span class="operator">></span>
<span class="operator"><</span>AttachingType<span class="operator">></span><span class="operator">.</span>on<span class="operator"><</span>SignalName<span class="operator">></span></pre>
<p>For example, the ListView type has an attached property ListView.isCurrentItem that is available to each delegate object in a ListView. This can be used by each individual delegate object to determine whether it is the currently selected item in the view:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">ListView</span> {
<span class="name">width</span>: <span class="number">240</span>; <span class="name">height</span>: <span class="number">320</span>
<span class="name">model</span>: <span class="number">3</span>
<span class="name">delegate</span>: <span class="name">Rectangle</span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>
<span class="name">color</span>: <span class="name">ListView</span>.<span class="name">isCurrentItem</span> ? <span class="string">"red"</span> : <span class="string">"yellow"</span>
}
}</pre>
<p>In this case, the name of the <i>attaching type</i> is <code>ListView</code> and the property in question is <code>isCurrentItem</code>, hence the attached property is referred to as <code>ListView.isCurrentItem</code>.</p>
<p>An attached signal handler is referred to in the same way. For example, the <a href="qml-qtqml-component.html#completed-signal">Component.onCompleted</a> attached signal handler is commonly used to execute some JavaScript code when a component's creation process has been completed. In the example below, once the <a href="qml-qtqml-models-listmodel.html">ListModel</a> has been fully created, its <code>Component.onCompleted</code> signal handler will automatically be invoked to populate the model:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">ListView</span> {
<span class="name">width</span>: <span class="number">240</span>; <span class="name">height</span>: <span class="number">320</span>
<span class="name">model</span>: <span class="name">ListModel</span> {
<span class="name">id</span>: <span class="name">listModel</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="number">10</span>; i++)
<span class="name">listModel</span>.<span class="name">append</span>({"Name": <span class="string">"Item "</span> <span class="operator">+</span> <span class="name">i</span>})
}
}
<span class="name">delegate</span>: <span class="name">Text</span> { <span class="name">text</span>: <span class="name">index</span> }
}</pre>
<p>Since the name of the <i>attaching type</i> is <code>Component</code> and that type has a <a href="qml-qtqml-component.html#completed-signal">completed</a> signal, the attached signal handler is referred to as <code>Component.onCompleted</code>.</p>
<a name="a-note-about-accessing-attached-properties-and-signal-handlers"></a>
<h4 >A Note About Accessing Attached Properties and Signal Handlers</h4>
<p>A common error is to assume that attached properties and signal handlers are directly accessible from the children of the object to which these attributes have been attached. This is not the case. The instance of the <i>attaching type</i> is only attached to specific objects, not to the object and all of its children.</p>
<p>For example, below is a modified version of the earlier example involving attached properties. This time, the delegate is an Item and the colored Rectangle is a child of that item:</p>
<pre class="qml">import QtQuick 2.0
<span class="type">ListView</span> {
<span class="name">width</span>: <span class="number">240</span>; <span class="name">height</span>: <span class="number">320</span>
<span class="name">model</span>: <span class="number">3</span>
<span class="name">delegate</span>: <span class="name">Item</span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>
<span class="type">Rectangle</span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>
<span class="name">color</span>: <span class="name">ListView</span>.<span class="name">isCurrentItem</span> ? <span class="string">"red"</span> : <span class="string">"yellow"</span> <span class="comment">// WRONG! This won't work.</span>
}
}
}</pre>
<p>This does not work as expected because <code>ListView.isCurrentItem</code> is attached <i>only</i> to the root delegate object, and not its children. Since the Rectangle is a child of the delegate, rather than being the delegate itself, it cannot access the <code>isCurrentItem</code> attached property as <code>ListView.isCurrentItem</code>. So instead, the rectangle should access <code>isCurrentItem</code> through the root delegate:</p>
<pre class="qml"><span class="type">ListView</span> {
<span class="comment">//....</span>
<span class="name">delegate</span>: <span class="name">Item</span> {
<span class="name">id</span>: <span class="name">delegateItem</span>
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>
<span class="type">Rectangle</span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">30</span>
<span class="name">color</span>: <span class="name">delegateItem</span>.<span class="name">ListView</span>.<span class="name">isCurrentItem</span> ? <span class="string">"red"</span> : <span class="string">"yellow"</span> <span class="comment">// correct</span>
}
}
}</pre>
<p>Now <code>delegateItem.ListView.isCurrentItem</code> correctly refers to the <code>isCurrentItem</code> attached property of the delegate.</p>
</div>
<!-- @@@qtqml-syntax-objectattributes.html -->
</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>
|