Make title capitalization more consistent in QML documentation.
[profile/ivi/qtdeclarative.git] / src / qml / doc / src / syntax / objectattributes.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
13 ** this file.
14 **
15 ** Other Usage
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
18 ** and Nokia.
19 **
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qtqml-syntax-objectattributes.html
30 \title QML Object Attributes
31 \brief Description of QML object type attributes
32
33 Every QML object type has a defined set of attributes. Each instance of an
34 object type is created with the set of attributes that have been defined for
35 that object type.  There are several different kinds of attributes which
36 can be specified, which are described below.
37
38 \section1 Attributes in Object Declarations
39
40 An \l{qtqml-syntax-basics.html#object-declarations}{object declaration} in a
41 QML document defines a new type.  It also declares an object hierarchy that
42 will be instantiated should an instance of that newly defined type be created.
43
44 The set of QML object-type attribute types is as follows:
45
46 \list
47 \li the \e id attribute
48 \li property attributes
49 \li signal attributes
50 \li signal handler attributes
51 \li method attributes
52 \li attached properties and attached signal handler attributes
53 \endlist
54
55 These attributes are discussed in detail below.
56
57 \section2 The \e id Attribute
58
59 Every QML object type has exactly one \e id attribute. This attribute is
60 provided by the language itself, and cannot be redefined or overridden by any
61 QML object type.
62
63 A value may be assigned to the \e id attribute of an object instance to allow
64 that object to be identified and referred to by other objects.  This \c id must
65 begin with a lower-case letter or an underscore, and cannot contain characters
66 other than letters, numbers and underscores.
67
68 Below is a \l TextInput object and a \l Text object. The \l TextInput object's
69 \c id value is set to "myTextInput". The \l Text object sets its \c text
70 property to have the same value as the \c text property of the \l TextInput,
71 by referring to \c myTextInput.text. Now, both items will display the same
72 text:
73
74 \qml
75 import QtQuick 2.0
76
77 Column {
78     width: 200; height: 200
79
80     TextInput { id: myTextInput; text: "Hello World" }
81
82     Text { text: myTextInput.text }
83 }
84 \endqml
85
86 An object can be referred to by its \c id from anywhere within the
87 \e {component scope} in which it is declared. Therefore, an \c id value must
88 always be unique within its component scope. See
89 \l{qtqml-documents-scope.html}{Scope and Naming Resolution} for more
90 information.
91
92 Once an object instance is created, the value of its \e id attribute cannot
93 be changed.  While it may look like an ordinary property, the \c id attribute
94 is \bold{not} an ordinary \c property attribute, and special semantics apply
95 to it; for example, it is not possible to access \c myTextInput.id in the above
96 example.
97
98
99 \section2 Property Attributes
100
101 A property is an attribute of an object that can be assigned a static value
102 or bound to a dynamic expression. A property's value can be read by other
103 objects. Generally it can also be modified by another object, unless a
104 particular QML type has explicitly disallowed this for a specific property.
105
106 \section3 Defining Property Attributes
107
108 A property may be defined for a type in C++ by registering a
109 Q_PROPERTY of a class which is then registered with the QML type system.
110 Alternatively, a custom property of an object type may be defined in
111 an object declaration in a QML document with the following syntax:
112
113 \code
114     [default] property <propertyType> <propertyName>
115 \endcode
116
117 In this way an object declaration may \l {Defining Object Types from QML}
118 {expose a particular value} to outside objects or maintain some internal
119 state more easily.
120
121 Property names must begin with a lower case letter and can only contain
122 letters, numbers and underscores. \l {JavaScript Reserved Words}
123 {JavaScript reserved words} are not valid property names.  The \c default
124 keyword is optional, and modifies the semantics of the property being declared.
125 See the upcoming section on \l {Default Properties}{default properties} for
126 more information about the \c default property modifier.
127
128 Declaring a custom property implicitly creates a value-change
129 \l{Signal attributes}{signal} for that property, as well as an associated
130 \l{Signal handler attributes}{signal handler} called
131 \e on<PropertyName>Changed, where \e <PropertyName> is the name of the
132 property, with the first letter capitalized.
133
134 For example, the following object declaration defines a new type which
135 derives from the Rectangle base type.  It has two new properties,
136 with a \l{Signal handler attributes}{signal handler} implemented for one of
137 those new properties:
138
139 \qml
140 Rectangle {
141     property color previousColor
142     property color nextColor
143     onNextColorChanged: console.log("The next color will be: " + nextColor.toString())
144 }
145 \endqml
146
147 \section4 Valid Types in Custom Property Definitions
148
149 The following types can be used as custom property types:
150
151 \list
152 \li \l bool
153 \li \l int
154 \li \l real, \l double
155 \li \l string
156 \li \l var
157 \endlist
158
159 The \l var type is a generic placeholder type that can hold any type of value,
160 including lists and objects:
161
162 \code
163 property var someNumber: 1.5
164 property var someString: "abc"
165 property var someBool: true
166 property var someList: [1, 2, "three", "four"]
167 property var someObject: Rectangle { width: 100; height: 100; color: "red" }
168 \endcode
169
170 For convenience, the following types can also be used as custom property types.
171 Using these types specifically instead of the \l var type provides type safety
172 and may also assist with application optimization:
173
174 \list
175 \li \l color
176 \li \l font
177 \li \l date
178 \li \l url
179 \li \l rect
180 \li \l point
181 \li \l size
182 \li \l vector2d
183 \li \l vector3d
184 \li \l vector4d
185 \li \l quaternion
186 \li \l matrix4x4
187 \endlist
188
189 \note Some of the above types are only available if the Qt Quick module has
190 been imported by the application.
191
192 Additionally, any \l{QML Object Types}{QML object type} can be used as a
193 property type. For example:
194
195 \code
196 property Item someItem
197 property Rectangle someRectangle
198 \endcode
199
200 This applies to \l {Defining Object Types from QML}{custom QML types} as well.
201 If a QML type was defined in a file named \c ColorfulButton.qml (in a directory
202 which was then imported by the client), then a property of type
203 \c ColorfulButton would also be valid.
204
205
206 \section3 Values of Property Attributes
207
208 The value of a property of an object instance may specified in an
209 object declaration in two separate ways:
210 \list
211   \li a value assignment on initialization
212   \li an imperative value assignment
213 \endlist
214
215 The value in either case may be either a binding expression or a static value.
216
217 \section4 Value Assignment on Initialization
218
219 The syntax for assigning a value to a property on initialization is:
220
221 \code
222     <propertyName> : <value>
223 \endcode
224
225 An initialization value assignment may be combined with a property definition
226 in an object declaration, if desired.  In that case, the syntax of the property
227 definition becomes:
228
229 \code
230     [default] property <propertyType> <propertyName> : <value>
231 \endcode
232
233 An example of property value initialization follows:
234
235 \qml
236 import QtQuick 2.0
237
238 Rectangle {
239     color: "red"
240     property color nextColor: "blue" // combined property declaration and initialization
241 }
242 \endqml
243
244 \section4 Imperative Value Assignment
245
246 An imperative value assignment is where a property value (either static value
247 or binding expression) is assigned to a property from imperative JavaScript
248 code.  The syntax of an imperative value assignment is just the JavaScript
249 assignment operator, as shown below:
250
251 \code
252     [<objectId>.]<propertyName> = value
253 \endcode
254
255 An example of imperative value assignment follows:
256
257 \qml
258 import QtQuick 2.0
259
260 Rectangle {
261     id: rect
262     Component.onCompleted: {
263         rect.color = "red"
264     }
265 }
266 \endqml
267
268 \section4 Valid Property Values
269
270 As previously noted, there are two kinds of values which may be assigned to a
271 property: static values, and binding expression values.
272
273 \table
274     \header
275     \li Kind
276     \li Semantics
277
278     \row
279     \li Static Value
280     \li A value whose type matches (or can be converted to) that of
281         the property may be assigned to the property.
282
283     \row
284     \li Binding Expression
285     \li A JavaScript expression which may be evaluated, whose result is a
286         value whose type matches (or can be converted to) that of the
287         property may be assigned to the property.  The expression will be
288         automatically re-evaluated (and the new result assigned to the
289         property) by the QML engine should the value of any properties accessed
290         during evaluation change.
291 \endtable
292
293 An example of these two types of values being assigned to properties follows:
294
295 \qml
296 import QtQuick 2.0
297
298 Rectangle {
299     // both of these are static value assignments on initialization
300     width: 400
301     height: 200
302
303     Rectangle {
304         // both of these are binding expression value assignments on initialization
305         width: parent.width / 2
306         height: parent.height
307     }
308 }
309 \endqml
310
311 In many cases, a string value may be converted automatically to a
312 different type of value, as QML provides string converters for many property
313 types (thus you can assign the string \c "red" to a color property).
314
315 It is important to note that in order to assign a binding expression to a
316 property in an imperative value assignment, the right-hand-side of
317 the assignment (the binding expression) must be a function returned by the
318 \l{Qt::binding()}{Qt.binding()} function, which returns a value of the
319 appropriate type.  A binding expression value may be assigned to a property
320 via an initialization value assignment without using that function (and, in
321 fact, attempting to do so will result in an error).  See the documentation
322 about \l{qtqml-syntax-propertybinding.html}{property binding} for more
323 information on the topic.
324
325
326 \section3 Type Safety
327
328 Properties are type safe. A property can only be assigned a value that matches
329 the property type.
330
331 For example, if a property is a real, and if you try to assign a string to it,
332 you will get an error:
333
334 \code
335 property int volume: "four"  // generates an error; the property's object will not be loaded
336 \endcode
337
338 Likewise if a property is assigned a value of the wrong type during run time,
339 the new value will not be assigned, and an error will be generated.
340
341 As noted in a previous section, some property types do not have a natural
342 value representation, and for those property types the QML engine
343 automatically performs string-to-typed-value conversion.  So, for example,
344 even though properties of the \c color type store colors and not strings,
345 you are able to assign the string \c "red" to a color property, without an
346 error being reported.
347
348 See \l {QML Basic Types} for a list of the types of properties that are
349 supported by default.  Additionally, any available \l {QML Object Types}
350 {QML object type} may also be used as a property type.
351
352 \section3 Special Property Types
353
354 \section4 Object List Property Attributes
355
356 A \l list type property can be assigned a list of QML object-type values.
357 The syntax for defining an object list value is a comma-separated list
358 surrounded by square brackets:
359
360 \code
361     [ <item 1>, <item 2>, ... ]
362 \endcode
363
364 For example, the \l Item type has a \l {Item::states}{states} property that is
365 used to hold a list of \l State type objects. The code below initializes the
366 value of this property to a list of three \l State objects:
367
368 \qml
369 import QtQuick 2.0
370
371 Item {
372     states: [
373         State { name: "loading" },
374         State { name: "running" },
375         State { name: "stopped" }
376     ]
377 }
378 \endqml
379
380 If the list contains a single item, the square brackets may be omitted:
381
382 \qml
383 import QtQuick 2.0
384
385 Item {
386     states: State { name: "running" }
387 }
388 \endqml
389
390 A \l list type property may be specified in an object declaration with the
391 following syntax:
392
393 \code
394     [default] property list<<objectType>> propertyName
395 \endcode
396
397 and, like other property declarations, a property initialization may be
398 combined with the property declaration with the following syntax:
399
400 \code
401     [default] property list<<objectType>> propertyName: <value>
402 \endcode
403
404 An example of list property declaration follows:
405
406 \qml
407 import QtQuick 2.0
408
409 Rectangle {
410     // declaration without initialization
411     property list<Rectangle> siblingRects
412
413     // declaration with initialization
414     property list<Rectangle> childRects: [
415         Rectangle { color: "red" },
416         Rectangle { color: "blue"}
417     ]
418 }
419 \endqml
420
421 If you wish to declare a property to store a list of values which are not
422 necessarily QML object-type values, you should declare a \l var property
423 instead.
424
425
426 \section4 Grouped Properties
427
428 In some cases properties contain a logical group of sub-property attributes.
429 These sub-property attributes can be assigned to using either the dot notation
430 or group notation.
431
432 For example, the \l Text type has a \l{Text::font}{font} group property. Below,
433 the first \l Text object initializes its \c font values using dot notation,
434 while the second uses group notation:
435
436 \code
437 Text {
438     //dot notation
439     font.pixelSize: 12
440     font.bold: true
441 }
442
443 Text {
444     //group notation
445     font { pixelSize: 12; bold: true }
446 }
447 \endcode
448
449 Grouped property types are basic types which have subproperties.  Some of these
450 basic types are provided by the QML language, while others may only be used if
451 the Qt Quick module is imported.  See the documentation about
452 \l{QML Basic Types} for more information.
453
454
455 \section3 Property Aliases
456
457 Property aliases are properties which hold a reference to another property.
458 Unlike an ordinary property definition, which allocates a new, unique storage
459 space for the property, a property alias connects the newly declared property
460 (called the aliasing property) as a direct reference to an existing property
461 (the aliased property).
462
463 A property alias declaration looks like an ordinary property definition, except
464 that it requires the \c alias keyword instead of a property type, and the
465 right-hand-side of the property declaration must be a valid alias reference:
466
467 \code
468 [default] property alias <name>: <alias reference>
469 \endcode
470
471 Unlike an ordinary property, an alias can only refer to a object, or the
472 property of a object, that is within the scope of the \l{QML Object Types}
473 {type} within which the alias is declared. It cannot contain arbitrary
474 JavaScript expressions and it cannot refer to objects declared outside of
475 the scope of its type. Also note the \e {alias reference} is not optional,
476 unlike the optional default value for an ordinary property; the alias reference
477 must be provided when the alias is first declared.
478
479 For example, below is a \c Button type with a \c buttonText aliased property
480 which is connected to the \c text object of the \l Text child:
481
482 \qml
483 // Button.qml
484 import QtQuick 2.0
485
486 Rectangle {
487     property alias buttonText: textItem.text
488
489     width: 100; height: 30; color: "yellow"
490
491     Text { id: textItem }
492 }
493 \endqml
494
495 The following code would create a \c Button with a defined text string for the
496 child \l Text object:
497
498 \qml
499 Button { buttonText: "Click Me" }
500 \endqml
501
502 Here, modifying \c buttonText directly modifies the textItem.text value; it
503 does not change some other value that then updates textItem.text. If
504 \c buttonText was not an alias, changing its value would not actually change
505 the displayed text at all, as property bindings are not bi-directional: the
506 \c buttonText value would have changed if textItem.text was changed, but not
507 the other way around.
508
509
510 \section4 Considerations for Property Aliases
511
512 Aliases are only activated once a component has been fully initialized. An
513 error is generated when an uninitialized alias is referenced. Likewise,
514 aliasing an aliasing property will also result in an error.
515
516 \snippet qml/properties.qml alias complete
517
518 When importing a \l{QML Object Types}QML object type} with a property alias in
519 the root object, however, the property appear as a regular Qt property and
520 consequently can be used in alias references.
521
522 It is possible for an aliasing property to have the same name as an existing
523 property, effectively overwriting the existing property. For example,
524 the following QML type has a \c color alias property, named the same as the
525 built-in \l {Rectangle::color} property:
526
527 \snippet qml/properties.qml alias overwrite
528
529 Any object that use this type and refer to its \c color property will be
530 referring to the alias rather than the ordinary \l {Rectangle::color} property.
531 Internally, however, the red can correctly set its \c color
532 property and refer to the actual defined property rather than the alias.
533
534
535 \section3 Default Properties
536
537 An object definition can have a single \e default property. A default property
538 is the property to which a value is assigned if an object is declared within
539 another object's definition without attaching it as a value to a particular
540 property.
541
542 Declaring a property with the optional \c default keyword marks it as the
543 default property. For example, say there is a file MyLabel.qml with a default
544 property \c someText:
545
546 \qml
547 // MyLabel.qml
548 import QtQuick 2.0
549
550 Text {
551     default property var someText
552
553     text: "Hello, " + someText.text
554 }
555 \endqml
556
557 The \c someText value could be assigned to in a \c MyLabel object definition,
558 like this:
559
560 \qml
561 MyLabel {
562     Text { text: "world!" }
563 }
564 \endqml
565
566 This has exactly the same effect as the following:
567
568 \qml
569 MyLabel {
570     someText: Text { text: "world!" }
571 }
572 \endqml
573
574 However, since the \c someText property has been marked as the default
575 property, it is not necessary to explicitly assign the \l Text object
576 to this property.
577
578 You will notice that child objects can be added to any \l {Item}-based type
579 without explicitly adding them to the \l {Item::children}{children} property.
580 This is because the default property of \l Item is its \c data property, and
581 any items added to this list for an \l Item are automatically added to its
582 list of \l {Item::children}{children}.
583
584 Default properties can be useful for reassigning the children of an item. See
585 the  \l{declarative/ui-components/tabwidget}{TabWidget example}, which uses a
586 default property to automatically reassign children of the TabWidget as
587 children of an inner ListView.
588
589
590 \section2 Signal Attributes
591
592 A signal is a notification from an object that some event has occurred: for
593 example, a property has changed, an animation has started or stopped, or
594 when an image has been downloaded. The \l MouseArea type, for example, has
595 a \l {MouseArea::clicked}{clicked} signal that is emitted when the user clicks
596 within the mouse area.
597
598 An object can be notified through a \l{Signal handler attributes}
599 {signal handler} whenever it a particular signal is emitted. A signal handler
600 is declared with the syntax \e on<Signal> where \e <Signal> is the name of the
601 signal, with the first letter capitalized. The signal handler must be declared
602 within the definition of the object that emits the signal, and the handler
603 should contain the block of JavaScript code to be executed when the signal
604 handler is invoked.
605
606 For example, the \e onClicked signal handler below is declared within the
607 \l MouseArea object definition, and is invoked when the \l MouseArea is
608 clicked, causing a console message to be printed:
609
610 \qml
611 import QtQuick 2.0
612
613 Item {
614     width: 100; height: 100
615
616     MouseArea {
617         anchors.fill: parent
618         onClicked: {
619             console.log("Click!")
620         }
621     }
622 }
623 \endqml
624
625 \section3 Defining Signal Attributes
626
627 A signal may be defined for a type in C++ by registering a Q_SIGNAL of a class
628 which is then registered with the QML type system.  Alternatively, a custom
629 signal for an object type may be defined in an object declaration in a QML
630 document with the following syntax:
631
632 \code
633     signal <signalName>[([<type> <parameter name>[, ...]])]
634 \endcode
635
636 Attempting to declare two signals or methods with the same name in the same
637 type block is an error. However, a new signal may reuse the name of an existing
638 signal on the type. (This should be done with caution, as the existing signal
639 may be hidden and become inaccessible.)
640
641 Here are three examples of signal declarations:
642
643 \qml
644 import QtQuick 2.0
645
646 Item {
647     signal clicked
648     signal hovered()
649     signal actionPerformed(string action, var actionResult)
650 }
651 \endqml
652
653 If the signal has no parameters, the "()" brackets are optional. If parameters
654 are used, the parameter types must be declared, as for the \c string and \c var
655 arguments for the \c actionPerformed signal above. The allowed parameter types
656 are the same as those listed under \l {custom property types} on this page.
657
658 To emit a signal, invoke it as a method. Any relevant
659 \l{Signal handler attributes}{signal handlers} will be invoked when the signal
660 is emitted, and handlers can use the defined signal argument names to access
661 the respective arguments.
662
663 \section3 Property Change Signals
664
665 QML types also provide built-in \e {property change signals} that are emitted
666 whenever a property value changes, as previously described in the section on
667 \l{Property attributes}{property attributes}.  See the upcoming section on
668 \l{Property change signal handlers}{property change signal handlers} for more
669 information about why these signals are useful, and how to use them.
670
671
672 \section2 Signal Handler Attributes
673
674 Signal handlers are a special sort of \l{Method attributes}{method attribute},
675 where the method implementation is invoked by the QML engine whenever the
676 associated signal is emitted.  Adding a signal to an object definition in QML
677 will automatically add an associated signal handler to the object definition,
678 which has, by default, an empty implementation.  Clients can provide an
679 implementation, to implement program logic.
680
681 Consider the following \c SquareButton type, whose definition is provided in
682 the \c SquareButton.qml file as shown below, with signals \c activated and
683 \c deactivated:
684
685 \qml
686 // SquareButton.qml
687 Rectangle {
688     id: root
689
690     signal activated(real xPosition, real yPosition)
691     signal deactivated
692
693     width: 100; height: 100
694
695     MouseArea {
696         anchors.fill: parent
697         onPressed: root.activated(mouse.x, mouse.y)
698         onRelased: root.deactivated()
699     }
700 }
701 \endqml
702
703 These signals could be received by any \c SquareButton objects in another QML
704 file in the same directory, where implementations for the signal handlers are
705 provided by the client:
706
707 \qml
708 // myapplication.qml
709 SquareButton {
710     onActivated: console.log("Activated at " + xPosition + "," + yPosition)
711     onDeactivated: console.log("Deactivated!")
712 }
713 \endqml
714
715 See the \l {Signal and Handler Event System} for more details on use of
716 signals.
717
718 \section3 Property Change Signal Handlers
719
720 Signal handlers for property change signal take the syntax form
721 \e on<Property>Changed where \e <Property> is the name of the property,
722 with the first letter capitalized. For example, although the \l TextInput type
723 documentation does not document a \c textChanged signal, this signal is
724 implicitly available through the fact that \l TextInput has a
725 \l {TextInput::text}{text} property and so it is possible to write an
726 \c onTextChanged signal handler to be called whenever this property changes:
727
728 \qml
729 import QtQuick 2.0
730
731 TextInput {
732     text: "Change this!"
733
734     onTextChanged: console.log("Text has changed to:", text)
735 }
736 \endqml
737
738
739 \section2 Method Attributes
740
741 A method of an object type is a function which may be called to perform some
742 processing or trigger further events.  A method can be connected to a signal so
743 that it is automatically invoked whenever the signal is emitted.  See
744 \l {Signal and Handler Event System} for more details.
745
746 \section3 Defining Method Attributes
747
748 A method may be defined for a type in C++ by tagging a function of a class
749 which is then registered with the QML type system with Q_INVOKABLE or by
750 registering it as a Q_SLOT of the class.  Alternatively, a custom method can
751 be added to an object declaration in a QML document with the following syntax:
752
753 \code
754     function <functionName>([<parameterName>[, ...]]) { <body> }
755 \endcode
756
757 Methods can be added to a QML type in order to define standalone, reusable
758 blocks of JavaScript code.  These methods can be invoked either internally or
759 by external objects.
760
761 Unlike signals, method parameter types do not have to be declared as they
762 default to the \c var type.
763
764 Attempting to declare two methods or signals with the same name in the same
765 type block is an error. However, a new method may reuse the name of an existing
766 method on the type. (This should be done with caution, as the existing method
767 may be hidden and become inaccessible.)
768
769 Below is a \l Rectangle with a \c calculateHeight() method that is called when
770 assigning the \c height value:
771
772 \qml
773 import QtQuick 2.0
774 Rectangle {
775     id: rect
776
777     function calculateHeight() {
778         return rect.width / 2;
779     }
780
781     width: 100
782     height: calculateHeight()
783 }
784 \endqml
785
786 If the method has parameters, they are accessible by name within the method.
787 Below, when the \l MouseArea is clicked it invokes the \c moveTo() method which
788 can then refer to the received \c newX and \c newY parameters to reposition the
789 text:
790
791 \qml
792 import QtQuick 2.0
793
794 Item {
795     width: 200; height: 200
796
797     MouseArea {
798         anchors.fill: parent
799         onClicked: label.moveTo(mouse.x, mouse.y)
800     }
801
802     Text {
803         id: label
804
805         function moveTo(newX, newY) {
806             label.x = newX;
807             label.y = newY;
808         }
809
810         text: "Move me!"
811     }
812 }
813 \endqml
814
815
816 \section2 Attached Properties and Attached Signal Handlers
817
818 \e {Attached properties} and \e {attached signal handlers} are mechanisms that
819 enable objects to be annotated with extra properties or signal handlers that
820 are otherwise unavailable to the object. In particular, they allow objects to
821 access properties or signals that are specifically relevant to the individual
822 object.
823
824 A QML type implementation may choose to create an \e {attaching type} with
825 particular properties and signals. Instances of this type can then be created
826 and \e attached to specific objects at run time, allowing those objects to
827 access the properties and signals of the attaching type. These are accessed by
828 prefixing the properties and respective signal handlers with the name of the
829 attaching type.
830
831 For example, the \l ListView type has an attached property
832 \l ListView.isCurrentItem that is available to each delegate object in a
833 ListView. This can be used by each individual delegate object to determine
834 whether it is the currently selected item in the view:
835
836 \qml
837 import QtQuick 2.0
838
839 ListView {
840     width: 240; height: 320
841     model: 3
842     delegate: Rectangle {
843         width: 100; height: 30
844         color: ListView.isCurrentItem ? "red" : "yellow"
845     }
846 }
847 \endqml
848
849 In this case, the name of the \e {attaching type} is \c ListView and the
850 property in question is \c isCurrentItem, hence the attached property is
851 referred to as \c ListView.isCurrentItem.
852
853 An attached signal handler is referred to in the same way. For example, the
854 \c Component.isCompleted attached signal handler is commonly used to execute
855 some JavaScript code when a component's creation process has been completed.
856 In the example below, once the \l ListModel has been fully created, its
857 \c Component.onCompleted signal handler will automatically be invoked to
858 populate the model:
859
860 \qml
861 import QtQuick 2.0
862
863 ListView {
864     width: 240; height: 320
865     model: ListModel {
866         id: listModel
867         Component.onCompleted: {
868             for (var i=0; i<10; i++)
869                 listModel.append({"Name": "Item " + i})
870         }
871     }
872     delegate: Text { text: index }
873 }
874 \endqml
875
876 Since the name of the \e {attaching type} is \c Component and that type has a
877 \c completed signal, the attached signal handler is referred to as
878 \c Component.isCompleted.
879
880
881 \section3 A Note About Accessing Attached Properties and Signal Handlers
882
883 A common error is to assume that attached properties and signal handlers are
884 directly accessible from the children of the object to which these attributes
885 have been attached. This is not the case. The instance of the
886 \e {attaching type} is only attached to specific objects, not to the object
887 and all of its children.
888
889 For example, below is a modified version of the earlier example involving
890 attached properties. This time, the delegate is an \l Item and the colored
891 \l Rectangle is a child of that item:
892
893 \qml
894 import QtQuick 2.0
895
896 ListView {
897     width: 240; height: 320
898     model: 3
899     delegate: Item {
900         width: 100; height: 30
901
902         Rectangle {
903             width: 100; height: 30
904             color: ListView.isCurrentItem ? "red" : "yellow"    // WRONG! This won't work.
905         }
906     }
907 }
908 \endqml
909
910 This does not work as expected because \c ListView.isCurrentItem is attached
911 \e only to the root delegate object, and not its children. Since the
912 \l Rectangle is a child of the delegate, rather than being the delegate itself,
913 it cannot access the \c isCurrentItem attached property as
914 \c ListView.isCurrentItem. So instead, the rectangle should access
915 \c isCurrentItem through the root delegate:
916
917 \qml
918 ListView {
919     //....
920     delegate: Item {
921         id: delegateItem
922         width: 100; height: 30
923
924         Rectangle {
925             width: 100; height: 30
926             color: delegateItem.ListView.isCurrentItem ? "red" : "yellow"   // correct
927         }
928     }
929 }
930 \endqml
931
932 Now \c delegateItem.ListView.isCurrentItem correctly refers to the
933 \c isCurrentItem attached property of the delegate.
934
935 */