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