53e5db429a2577095193bde8585b5b75cd3c46ae
[profile/ivi/qtdeclarative.git] / doc / src / declarative / extending.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
14 ** this file.
15 **
16 ** Other Usage
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
19 ** and Nokia.
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qml-extending.html
30 \inqmlmodule QtQuick 2
31 \ingroup qml-features
32 \contentspage QML Features
33 \previouspage {Presenting Data with Views}
34 \nextpage {Using QML Bindings in C++ Applications}
35 \title Extending QML Functionalities using C++
36
37 The QML syntax declaratively describes how to construct an in-memory object
38 tree.  In Qt, QML is mainly used to describe a visual scene graph, but it is
39 not conceptually limited to this: the QML format is an abstract description of
40 any object tree.  All the QML element types included in Qt are implemented using
41 the C++ extension mechanisms describe on this page.  Programmers can use these
42 APIs to add new types that interact with the existing Qt types, or to repurpose
43 QML for their own independent use.
44
45 \tableofcontents
46
47 \section1 Adding Types
48 \target adding-types
49
50 \snippet examples/declarative/cppextensions/referenceexamples/adding/example.qml 0
51
52 The QML snippet shown above instantiates one \c Person instance and sets
53 the \c name and \c shoeSize properties on it.  Everything in QML ultimately comes down
54 to either instantiating an object instance, or assigning a property a value.
55
56 QML relies heavily on Qt's meta object system and can only instantiate classes
57 that derive from QObject. For visual element types, this will usually mean a subclass
58 of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel;
59 and for arbitrary objects with properties, a direct subclass of QObject.
60
61 The QML engine has no intrinsic knowledge of any class types.  Instead the
62 programmer must register the C++ types with their corresponding QML names.
63
64 Custom C++ types are registered using a template function:
65
66 \quotation
67
68 \code
69 template<typename T>
70 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
71 \endcode
72
73 Calling qmlRegisterType() registers the C++ type \a T with the QML
74 system, and makes it available in QML under the name \a qmlName in
75 library \a uri version \a versionMajor.versionMinor.  The \a qmlName
76 can be the same as the C++ type name.
77
78 Type \a T must be a concrete type that inherits QObject and has a default
79 constructor.
80
81 \endquotation
82
83 #include <QtDeclarative> to use qmlRegisterType().
84
85 Types can be registered by libraries, application code, or by plugins
86 (see QDeclarativeExtensionPlugin).
87
88 Once registered, all \l {Qt's Property System}{properties} of the
89 supported types are available in QML.  QML has intrinsic support for
90 properties of the types listed in the \l{QML Basic Types}
91 document, which includes the following:
92
93 \list
94 \o bool, unsigned int, int, float, double, qreal
95 \o QString, QUrl, QColor
96 \o QDate, QTime, QDateTime
97 \o QPoint, QPointF, QSize, QSizeF, QRect, QRectF
98 \o QVariant
99 \endlist
100
101 When a property of a supported type is added to a C++ class, in a QML
102 element based on the C++ class, a \i{value-changed} signal handler
103 will be available. See \l{Signal Support} below.
104
105 QML is typesafe.  Attempting to assign an invalid value to a property
106 will generate an error.  For example, assuming the \i{name} property
107 of the \c Person element had a type of QString, this would cause an
108 error:
109
110 \code
111 Person {
112     // Will NOT work
113     name: 12
114 }
115 \endcode
116
117 \l {Extending QML - Adding Types Example} shows the complete code used to create
118 the \c Person type.
119
120 If you wish to provide functionality to clients without requiring them to
121 instantiate an element, consider providing a module API instead of registering
122 a type; see qmlRegisterModuleApi() for more information.
123
124 \section1 QML Type Versioning
125
126 In C++ adding a new class, method or property cannot break old applications.
127 In QML, however, new elements, methods and properties can change what a name previously
128 resolved to to within a scope chain.
129
130 For example, consider these two QML files
131
132 \code
133 // main.qml
134 import QtQuick 1.0
135 Item {
136     id: root
137     MyComponent {}
138 }
139 \endcode
140
141 \code
142 // MyComponent.qml
143 import MyModule 1.0
144 CppElement {
145     value: root.x
146 }
147 \endcode
148
149 where CppElement maps to the C++ class QCppElement.
150
151 If the author of QCppElement adds a "root" property to QCppElement in a new version of the module,
152 it will break the above program as \c root.x now resolves to a different value.
153 The solution is to allow the author of QCppElement to state that the new \c root property is
154 only available from a particular version of QCppElement onwards. This permits new properties
155 and features to be added to existing elements without breaking existing programs. 
156
157 QML enables this by allowing the properties, methods and signals of a class to be tagged with
158 a particular \i revision, so that they are only accessible if the relevant module version
159 is imported.  In this case, the author can tag the \c root property as being added in
160 \i {revision 1} of the class, and register that revision in version 1.1 of the module.
161
162 The REVISION tag is used to mark the \c root property as added in revision 1 of the class.
163 Methods such as Q_INVOKABLE's, signals and slots can also be tagged for a
164 revision using the \c Q_REVISION(x) macro:
165
166 \code
167 class CppElement : public BaseObject
168 {
169     Q_OBJECT
170     Q_PROPERTY(int root READ root WRITE setRoot NOTIFY rootChanged REVISION 1)
171
172 signals:
173     Q_REVISION(1) void rootChanged();
174 };
175 \endcode
176
177 To register the new class revision to a particular version the following function is used:
178
179 \code
180 template<typename T, int metaObjectRevision>
181 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
182 \endcode
183
184 To register \c CppElement version 1 for \c {MyModule 1.1}:
185
186 \code
187 qmlRegisterType<QCppElement,1>("MyModule", 1, 1, "CppElement")
188 \endcode
189
190 \c root is only available when MyModule 1.1 is imported.
191
192 For the same reason, new elements introduced in later versions should use the minor version argument of qmlRegisterType.
193
194 This feature of the language allows for behavioural changes to be made without breaking existing applications. Consequently QML module authors should always remember to document what changed between minor versions, and QML module users should check that their application still runs correctly before deploying an updated import statement.
195
196 You may also register the revision of a base class that your module depends upon
197 using the qmlRegisterRevision() function:
198
199 \code
200 template<typename T, int metaObjectRevision>
201 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
202 \endcode
203
204 For example, if \c BaseObject is changed and now has a revision 1, you can specify that
205 your module uses the new revision:
206
207 \code
208 qmlRegisterRevision<BaseObject,1>("MyModule", 1, 1);
209 \endcode
210
211 This is useful when deriving from base classes not declared as part of your module, e.g.
212 when extending classes from the QtQuick library.
213
214
215 \section1 Object and List Property Types
216
217 \snippet examples/declarative/cppextensions/referenceexamples/properties/example.qml 0
218
219 The QML snippet shown above assigns a \c Person object to the \c BirthdayParty's
220 \c host property, and assigns three \c Person objects to the guests property.
221
222 QML can set properties of types that are more complex than basic intrinsics like
223 integers and strings.  Properties can also be object pointers, Qt interface
224 pointers,  lists of object pointers, and lists of Qt interface pointers.  As QML
225 is typesafe it ensures that only valid types are assigned to these properties,
226 just like it does for primitive types.
227
228 Properties that are pointers to objects or Qt interfaces are declared with the
229 Q_PROPERTY() macro, just like other properties.  The \c host property
230 declaration looks like this:
231
232 \snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 1
233
234 As long as the property type, in this case \c Person, is registered with QML the
235 property can be assigned.
236
237 QML also supports assigning Qt interfaces.  To assign to a property whose type
238 is a Qt interface pointer, the interface must also be registered with QML.  As
239 they cannot be instantiated directly, registering a Qt interface is different
240 from registering a new QML type. The following function is used instead:
241
242 \quotation
243 \code
244 template<typename T>
245 int qmlRegisterInterface(const char *typeName)
246 \endcode
247
248 This registers the C++ interface \a T with the QML system as \a typeName.
249
250 Following registration, QML can coerce objects that implement this interface
251 for assignment to appropriately typed properties.
252 \endquotation
253
254 The \c guests property is a list of \c Person objects.  Properties that are lists
255 of objects or Qt interfaces are also declared with the Q_PROPERTY() macro, just
256 like other properties.  List properties must have the type \c {QDeclarativeListProperty<T>}.
257 As with object properties, the type \a T must be registered with QML.
258
259 The \c guest property declaration looks like this:
260
261 \snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 2
262
263 \l {Extending QML - Object and List Property Types Example} shows the complete
264 code used to create the \c BirthdayParty type.
265
266 \section1 Sequence Types
267
268 Certain C++ sequence types are supported transparently in QML as JavaScript Array types.
269 In particular, QML currently supports:
270 \list
271   \o \c {QList<int>}
272   \o \c {QList<qreal>}
273   \o \c {QList<bool>}
274   \o \c {QList<QString>} and \c{QStringList}
275   \o \c {QList<QUrl>}
276 \endlist
277
278 These sequence types are implemented directly in terms of the underlying C++ sequence.
279 There are two ways in which such sequences can be exposed to QML: as a Q_PROPERTY of
280 the given sequence type; or as the return type of a Q_INVOKABLE method.  There are some
281 differences in the way these are implemented, which are important to note.
282
283 If the sequence is exposed as a Q_PROPERTY, accessing any value in the sequence by index
284 will cause the sequence data to be read from the QObject's property, then a read to occur.
285 Similarly, modifying any value in the sequence will cause the sequence data to be read,
286 and then the modification will be performed and the modified sequence will be written back
287 to the QObject's property.
288
289 If the sequence is returned from a Q_INVOKABLE function, access and mutation is much cheaper,
290 as no QObject property read or write occurs; instead, the C++ sequence data is accessed and
291 modified directly.
292
293 Other sequence types are not supported transparently, and instead an instance of any other
294 sequence type will be passed between QML and C++ as an opaque QVariantList.
295
296 \bold {Important Note:} There are some minor differences between the semantics of such
297 sequence Array types and default JavaScript Array types which result from the use of a
298 C++ storage type in the implementation.  In particular, deleting an element from an Array
299 will result in a default-constructed value replacing that element, rather than an
300 Undefined value.  Similarly, setting the length property of the Array to a value larger
301 than its current value will result in the Array being padded out to the specified length
302 with default-constructed elements rather than Undefined elements.
303
304 The default-constructed values for each sequence type are as follows:
305 \table
306 \row \o QList<int> \o integer value 0
307 \row \o QList<qreal> \o real value 0.0
308 \row \o QList<bool> \o boolean value \c {false}
309 \row \o QList<QString> and QStringList \o empty QString
310 \row \o QList<QUrl> \o empty QUrl
311 \endtable
312
313 If you wish to remove elements from a sequence rather than simply replace them with default
314 constructed values, do not use the indexed delete operator ("delete sequence[i]") but instead
315 use the \c {splice} function ("sequence.splice(startIndex, deleteCount)").
316
317 \section1 Inheritance and Coercion
318
319 \snippet examples/declarative/cppextensions/referenceexamples/coercion/example.qml 0
320
321 The QML snippet shown above assigns a \c Boy object to the \c BirthdayParty's
322 \c host property, and assigns three other objects to the \c guests property.
323
324 QML supports C++ inheritance hierarchies and can freely coerce between known,
325 valid object types.  This enables the creation of common base classes that allow
326 the assignment of specialized classes to object or list properties.  In the
327 snippet shown, both the \c host and the \c guests properties retain the \c Person
328 type used in the previous section, but the assignment is valid as both the \c Boy
329 and \c Girl objects inherit from \c Person.
330
331 To assign to a property, the property's type must have been registered with QML.
332 Both the qmlRegisterType() and qmlRegisterInterface() template functions already
333 shown can be used to register a type with QML.  Additionally, if a type that acts purely
334 as a base class that cannot be instantiated from QML needs to be
335 registered, the following function can be used:
336
337 \quotation
338 \code
339     template<typename T>
340     int qmlRegisterType()
341 \endcode
342
343 This registers the C++ type \a T with the QML system. The parameterless call to the template
344 function qmlRegisterType() does not define a mapping between the
345 C++ class and a QML element name, so the type is not instantiable from QML, but
346 it is available for type coercion.
347
348 Type \a T must inherit QObject, but there are no restrictions on whether it is
349 concrete or the signature of its constructor.
350 \endquotation
351
352 QML will automatically coerce C++ types when assigning to either an object
353 property, or to a list property.  Only if coercion fails does an assignment
354 error occur.
355
356 \l {Extending QML - Inheritance and Coercion Example} shows the complete
357 code used to create the \c Boy and \c Girl types.
358
359 \section1 Default Property
360
361 \snippet examples/declarative/cppextensions/referenceexamples/default/example.qml 0
362
363 The QML snippet shown above assigns a collection of objects to the
364 \c BirthdayParty's default property.
365
366 The \i {default property} is a syntactic convenience that allows a type designer to
367 specify a single property as the type's default.  The default property is
368 assigned to whenever no explicit property is specified.  As a convenience, it is
369 behaviorally identical to assigning to the default property explicitly by name.
370
371 From C++, type designers mark the default property using a Q_CLASSINFO()
372 annotation:
373
374 \quotation
375 \code
376 Q_CLASSINFO("DefaultProperty", "property")
377 \endcode
378
379 This marks \a property as the class's default property.  \a property must be either
380 an object property, or a list property.
381
382 A default property is optional.  A derived class inherits its base class's
383 default property, but may override it in its own declaration.  \a property can
384 refer to a property declared in the class itself, or a property inherited from a
385 base class.
386 \endquotation
387
388 \l {Extending QML - Default Property Example} shows the complete code used to
389 specify a default property.
390
391 \section1 Grouped Properties
392
393 \snippet examples/declarative/cppextensions/referenceexamples/grouped/example.qml 1
394
395 The QML snippet shown above assigns a number of properties to the \c Boy object,
396 including four properties using the grouped property syntax.
397
398 Grouped properties collect similar properties together into a single named
399 block.  Grouped properties can be used to present a nicer API to developers, and
400 may also simplify the implementation of common property collections across
401 different types through implementation reuse.
402
403 A grouped property block is implemented as a read-only object property.  The
404 \c shoe property shown is declared like this:
405
406 \snippet examples/declarative/cppextensions/referenceexamples/grouped/person.h 1
407
408 The \c ShoeDescription type declares the properties available to the grouped
409 property block - in this case the \c size, \c color, \c brand and \c price properties.
410
411 Grouped property blocks may declared and accessed be recusively.
412
413 \l {Extending QML - Grouped Properties Example} shows the complete code used to
414 implement the \c shoe property grouping.
415
416 \section1 Attached Properties
417
418 \snippet examples/declarative/cppextensions/referenceexamples/attached/example.qml 1
419
420 The QML snippet shown above assigns a date to the \c rsvp property using the attached
421 property syntax.
422
423 Attached properties allow unrelated types to annotate other types with some
424 additional properties, generally for their own use.  Attached properties are
425 identified through the use of the attacher type name, in the case shown
426 \c BirthdayParty, as a prefix to the property name.
427
428 In the example shown, \c BirthdayParty is called the attaching type, and the
429 \c Boy instance the attachee object instance.
430
431 For the attaching type, an attached property block is implemented as a new
432 QObject derived type, called the attachment object.  The properties on the
433 attachment object are those that become available for use as the attached
434 property block.
435
436 Any QML type can become an attaching type by declaring the
437 \c qmlAttachedProperties() public function and declaring that the class has
438 QML_HAS_ATTACHED_PROPERTIES:
439
440 \quotation
441 \code
442 class MyType : public QObject {
443     Q_OBJECT
444 public:
445
446     ...
447
448     static AttachedPropertiesType *qmlAttachedProperties(QObject *object);
449 };
450
451 QML_DECLARE_TYPEINFO(MyType, QML_HAS_ATTACHED_PROPERTIES)
452 \endcode
453 This returns an attachment object, of type \a AttachedPropertiesType, for the
454 attachee \a object instance.  It is customary, though not strictly required, for
455 the attachment object to be parented to \a object to prevent memory leaks.
456
457 \a AttachedPropertiesType must be a QObject derived type.  The properties on
458 this type will be accessible through the attached properties syntax.
459
460 This method will be called at most once for each attachee object instance.  The
461 QML engine will cache the returned instance pointer for subsequent attached
462 property accesses.  Consequently the attachment object may not be deleted until
463 \a object is destroyed.
464 \endquotation
465
466 Conceptually, attached properties are a \i type exporting a set of additional
467 properties that can be set on \i any other object instance.  Attached properties
468 cannot be limited to only attaching to a sub-set of object instances, although
469 their effect may be so limited.
470
471 For example, a common usage scenario is for a type to enhance the properties
472 available to its children in order to gather instance specific data.  Here we
473 add a \c rsvp field to all the guests coming to a birthday party:
474 \code
475 BirthdayParty {
476     Boy { BirthdayParty.rsvp: "2009-06-01" }
477 }
478 \endcode
479 However, as a type cannot limit the instances to which the attachment object
480 must attach, the following is also allowed, even though adding a birthday party
481 rsvp in this context will have no effect.
482 \code
483 GraduationParty {
484     Boy { BirthdayParty.rsvp: "2009-06-01" }
485 }
486 \endcode
487
488 From C++, including the attaching type implementation, the attachment object for
489 an instance can be accessed using the following method:
490
491 \quotation
492 \code
493 template<typename T>
494 QObject *qmlAttachedPropertiesObject<T>(QObject *attachee, bool create = true);
495 \endcode
496 This returns the attachment object attached to \a attachee by the attaching type
497 \a T.  If type \a T is not a valid attaching type, this method always returns 0.
498
499 If \a create is true, a valid attachment object will always be returned,
500 creating it if it does not already exist.  If \a create is false, the attachment
501 object will only be returned if it has previously been created.
502 \endquotation
503
504 \l {Extending QML - Attached Properties Example} shows the complete code used to
505 implement the rsvp attached property.
506
507 \section1 Memory Management and QVariant types
508
509 It is an element's responsibility to ensure that it does not access or return
510 pointers to invalid objects.  QML makes the following guarentees:
511
512 \list
513 \o An object assigned to a QObject (or QObject-derived) pointer property will be
514 valid at the time of assignment.
515
516 Following assignment, it is the responsibility of the class to subsequently guard
517 this pointer, either through a class specific method or the generic QPointer class.
518
519 \o An object assigned to a QVariant will be valid at the time of assignment.
520
521 When assigning an object to a QVariant property, QML will always use a QMetaType::QObjectStar
522 typed QVariant.  It is the responsibility of the class to guard the pointer.  A
523 general rule when writing a class that uses QVariant properties is to check the
524 type of the QVariant when it is set and if the type is not handled by your class,
525 reset it to an invalid variant.
526
527 \o An object assigned to a QObject (or QObject-derived) list property will be
528 valid at the time of assignment.
529
530 Following assignment, it is the responsibility of the class to subsequently guard
531 this pointer, either through a class specific method or the generic QPointer class.
532 \endlist
533
534 Elements should assume that any QML assigned object can be deleted at any time, and
535 respond accordingly.  If documented as such an element need not continue to work in
536 this situation, but it must not crash.
537
538 \section1 Signal Support
539
540 \snippet examples/declarative/cppextensions/referenceexamples/signal/example.qml 0
541 \snippet examples/declarative/cppextensions/referenceexamples/signal/example.qml 1
542
543 The QML snippet shown above associates the evaluation of a JavaScript expression
544 with the emission of a Qt signal.
545
546 All Qt signals on a registered class become available as special "signal
547 properties" within QML to which the user can assign a single JavaScript
548 expression.  The signal property's name is a transformed version of the Qt
549 signal name: "on" is prepended, and the first letter of the signal name upper
550 cased.  For example, the signal used in the example above has the following
551 C++ signature:
552
553 \snippet examples/declarative/cppextensions/referenceexamples/signal/birthdayparty.h 0
554
555 In classes with multiple signals with the same name, only the final signal
556 is accessible as a signal property.  Note that signals with the same name
557 but different parameters cannot be distinguished.
558
559 Signal parameters become accessible by name to the assigned script.  An
560 unnamed parameter cannot be accessed, so care should be taken to name all the
561 signal parameters in the C++ class declaration.  The intrinsic types
562 listed in \l{Adding Types}, as well registered object types are permitted as
563 signal parameter types.  Using other types is not an error, but the parameter
564 value will not be accessible from script.
565
566 \l {Extending QML - Signal Support Example} shows the complete code used to
567 implement the onPartyStarted signal property.
568
569 If you want to use signals from objects not created in QML, you can access their
570 signals with the \l {Connections} element.
571
572 Additionally, if a property is added to a C++ class, all QML elements
573 based on that C++ class will have a \i{value-changed} signal handler
574 for that property. The name of the signal handler is
575 \i{on<Property-name>Changed}, with the first letter of the property
576 name being upper case.
577
578 \note The QML signal handler will always be named
579 on<Property-name>Changed, regardless of the name used for the NOTIFY
580 signal in C++. We recommend using <property-name>Changed() for the
581 NOTIFY signal in C++.
582
583 See also \l {Importing Reusable Components}
584
585 \section1 Methods
586
587 Slots and methods marked Q_INVOKABLE may be called as functions in QML.
588
589 \snippet examples/declarative/cppextensions/referenceexamples/methods/example.qml 0
590
591 In this example an invitation is added via an \c {invite()} invokable method of
592 the BirthdayParty element.  This function is available in QML by marking the \c {invite()}
593 method with Q_INVOKABLE in the BirthdayParty class:
594
595 \snippet examples/declarative/cppextensions/referenceexamples/methods/birthdayparty.h 0
596
597 \l {Extending QML - Methods Example} shows the complete code used to
598 implement the invite() method.
599
600 The \c {invite()} method is similarly available if it is declared as a slot.
601
602 \section1 Property Value Sources
603
604 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/example.qml 0
605 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/example.qml 1
606
607 The QML snippet shown above applies a property value source to the \c announcement property.
608 A property value source generates a value for a property that changes over time.
609
610 Property value sources are most commonly used to do animation.  Rather than
611 constructing an animation object and manually setting the animation's "target"
612 property, a property value source can be assigned directly to a property of any
613 type and automatically set up this association.
614
615 The example shown here is rather contrived: the \c announcement property of the
616 \c BirthdayParty object is a string that is printed every time it is assigned and
617 the \c HappyBirthdaySong value source generates the lyrics of the song
618 "Happy Birthday".
619
620 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/birthdayparty.h 0
621
622 Normally, assigning an object to a string property would not be allowed.  In
623 the case of a property value source, rather than assigning the object instance
624 itself, the QML engine sets up an association between the value source and
625 the property.
626
627 Property value sources are special types that derive from the
628 QDeclarativePropertyValueSource base class.  This base class contains a single method,
629 QDeclarativePropertyValueSource::setTarget(), that the QML engine invokes when
630 associating the property value source with a property.  The relevant part of
631 the \c HappyBirthdaySong type declaration looks like this:
632
633 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/happybirthdaysong.h 0
634 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/happybirthdaysong.h 1
635 \snippet examples/declarative/cppextensions/referenceexamples/valuesource/happybirthdaysong.h 2
636
637 In all other respects, property value sources are regular QML types.  They must
638 be registered with the QML engine using the same macros as other types, and can
639 contain properties, signals and methods just like other types.
640
641 When a property value source object is assigned to a property, QML first tries
642 to assign it normally, as though it were a regular QML type.  Only if this
643 assignment fails does the engine call the \l {QDeclarativePropertyValueSource::}{setTarget()} method.  This allows
644 the type to also be used in contexts other than just as a value source.
645
646 \l {Extending QML -  Property Value Source Example} shows the complete code used
647 to implement the \c HappyBirthdaySong property value source.
648
649 \section1 Property Binding
650
651 \snippet examples/declarative/cppextensions/referenceexamples/binding/example.qml 0
652 \snippet examples/declarative/cppextensions/referenceexamples/binding/example.qml 1
653
654 The QML snippet shown above uses a property binding to ensure the
655 \c HappyBirthdaySong's \c name property remains up to date with the \c host.
656
657 Property binding is a core feature of QML.  In addition to assigning literal
658 values, property bindings allow the developer to assign an arbitrarily complex
659 JavaScript expression that may include dependencies on other property values.
660 Whenever the expression's result changes - through a change in one of its
661 constituent values - the expression is automatically reevaluated and
662 the new result assigned to the property.
663
664 All properties on custom types automatically support property binding.  However,
665 for binding to work correctly, QML must be able to reliably determine when a
666 property has changed so that it knows to reevaluate any bindings that depend on
667 the property's value.  QML relies on the presence of a
668 \l {Qt's Property System}{NOTIFY signal} for this determination.
669
670 Here is the \c host property declaration:
671
672 \snippet examples/declarative/cppextensions/referenceexamples/binding/birthdayparty.h 0
673
674 The NOTIFY attribute is followed by a signal name.  It is the responsibility of
675 the class implementer to ensure that whenever the property's value changes, the
676 NOTIFY signal is emitted.  The signature of the NOTIFY signal is not important to QML.
677
678 To prevent loops or excessive evaluation, developers should ensure that the
679 signal is only emitted whenever the property's value is actually changed.  If
680 a property, or group of properties, is infrequently used it is permitted to use
681 the same NOTIFY signal for several properties.  This should be done with care to
682 ensure that performance doesn't suffer.
683
684 To keep QML reliable, if a property does not have a NOTIFY signal, it cannot be
685 used in a binding expression.  However, the property can still be assigned
686 a binding as QML does not need to monitor the property for change in that
687 scenario.
688
689 Consider a custom type, \c TestElement, that has two properties, "a" and "b".
690 Property "a" does not have a NOTIFY signal, and property "b" does have a NOTIFY
691 signal.
692
693 \code
694 TestElement {
695     // This is OK
696     a: b
697 }
698 TestElement {
699     // Will NOT work
700     b: a
701 }
702 \endcode
703
704 The presence of a NOTIFY signal does incur a small overhead.  There are cases
705 where a property's value is set at object construction time, and does not
706 subsequently change.  The most common case of this is when a type uses
707 \l {Grouped Properties}, and the grouped property object is allocated once, and
708 only freed when the object is deleted.  In these cases, the CONSTANT attribute
709 may be added to the property declaration instead of a NOTIFY signal.
710
711 \snippet examples/declarative/cppextensions/referenceexamples/binding/person.h 0
712
713 Extreme care must be taken here or applications using your type may misbehave.
714 The CONSTANT attribute should only be used for properties whose value is set,
715 and finalized, only in the class constructor.  All other properties that want
716 to be used in bindings should have a NOTIFY signal instead.
717
718 \l {Extending QML -  Binding Example} shows the BirthdayParty example updated to
719 include NOTIFY signals for use in binding.
720
721 \section1 Extension Objects
722
723 \snippet examples/declarative/cppextensions/referenceexamples/extended/example.qml 0
724
725 The QML snippet shown above adds a new property to an existing C++ type without
726 modifying its source code.
727
728 When integrating existing classes and technology into QML, their APIs will often
729 need to be tweaked to fit better into the declarative environment.  Although
730 the best results are usually obtained by modifying the original classes
731 directly, if this is either not possible or is complicated by some other
732 concerns, extension objects allow limited extension possibilities without
733 direct modifications.
734
735 Extension objects are used to add additional properties to an existing type.
736 Extension objects can only add properties, not signals or methods.  An extended
737 type definition allows the programmer to supply an additional type - known as the
738 extension type - when registering the target class whose properties are
739 transparently merged with the original target class when used from within QML.
740
741 An extension class is a regular QObject, with a constructor that takes a QObject
742 pointer.  When needed (extension class creation is delayed until the first extended
743 property is accessed) the extension class is created and the target object is
744 passed in as the parent.  When an extended property on the original is accessed,
745 the appropriate property on the extension object is used instead.
746
747 When an extended type is installed, one of the
748 \code
749 template<typename T, typename ExtendedT>
750 int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
751
752 template<typename T, typename ExtendedT>
753 int qmlRegisterExtendedType()
754 \endcode
755 functions should be used instead of the regular \c qmlRegisterType() variations.
756 The arguments are identical to the corresponding non-extension registration functions,
757 except for the ExtendedT parameter which is the type
758 of the extension object.
759
760 \section1 Optimization
761
762 Often to develop high performance elements it is helpful to know more about the
763 status of the QML engine. For example, it might be beneficial to delay
764 initializing some costly data structures until after all the properties have been
765 set.
766
767 The QML engine defines an interface class called QDeclarativeParserStatus, which contains a
768 number of virtual methods that are invoked at various stages during component
769 instantiation.  To receive these notifications, an element implementation inherits
770 QDeclarativeParserStatus and notifies the Qt meta system using the Q_INTERFACES() macro.
771
772 For example,
773
774 \code
775 class Example : public QObject, public QDeclarativeParserStatus
776 {
777     Q_OBJECT
778     Q_INTERFACES(QDeclarativeParserStatus)
779 public:
780     virtual void componentComplete()
781     {
782         qDebug() << "Woohoo!  Now to do my costly initialization";
783     }
784 };
785 \endcode
786 */