From 61f3cb6e79fea0aed80df091013c2228f64955ec Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Wed, 21 Apr 2010 10:58:02 +1000 Subject: Improve docs and examples for Extending QML in C++ --- doc/src/declarative/extending.qdoc | 385 +++++++++++---------- examples/declarative/extending/adding/example.qml | 2 +- examples/declarative/extending/adding/main.cpp | 3 +- examples/declarative/extending/adding/person.h | 12 +- .../extending/attached/birthdayparty.cpp | 10 +- .../declarative/extending/attached/birthdayparty.h | 18 +- .../declarative/extending/attached/example.qml | 10 +- examples/declarative/extending/attached/main.cpp | 6 +- examples/declarative/extending/attached/person.h | 33 +- examples/declarative/extending/binding/binding.pro | 4 +- .../extending/binding/birthdayparty.cpp | 18 +- .../declarative/extending/binding/birthdayparty.h | 26 +- examples/declarative/extending/binding/example.qml | 6 +- .../extending/binding/happybirthday.cpp | 86 ----- .../declarative/extending/binding/happybirthday.h | 76 ---- .../extending/binding/happybirthdaysong.cpp | 86 +++++ .../extending/binding/happybirthdaysong.h | 75 ++++ examples/declarative/extending/binding/main.cpp | 10 +- examples/declarative/extending/binding/person.h | 33 +- .../extending/coercion/birthdayparty.cpp | 10 +- .../declarative/extending/coercion/birthdayparty.h | 14 +- .../declarative/extending/coercion/example.qml | 4 +- examples/declarative/extending/coercion/main.cpp | 6 +- examples/declarative/extending/coercion/person.h | 20 +- .../extending/default/birthdayparty.cpp | 10 +- .../declarative/extending/default/birthdayparty.h | 16 +- examples/declarative/extending/default/example.qml | 4 +- examples/declarative/extending/default/main.cpp | 6 +- examples/declarative/extending/default/person.h | 20 +- examples/declarative/extending/extended/lineedit.h | 10 +- .../extending/grouped/birthdayparty.cpp | 10 +- .../declarative/extending/grouped/birthdayparty.h | 16 +- examples/declarative/extending/grouped/example.qml | 4 +- examples/declarative/extending/grouped/main.cpp | 6 +- examples/declarative/extending/grouped/person.h | 33 +- .../extending/properties/birthdayparty.cpp | 10 +- .../extending/properties/birthdayparty.h | 14 +- .../declarative/extending/properties/example.qml | 4 +- examples/declarative/extending/properties/main.cpp | 4 +- examples/declarative/extending/properties/person.h | 10 +- .../declarative/extending/signal/birthdayparty.cpp | 10 +- .../declarative/extending/signal/birthdayparty.h | 18 +- examples/declarative/extending/signal/example.qml | 4 +- examples/declarative/extending/signal/main.cpp | 6 +- examples/declarative/extending/signal/person.h | 33 +- .../extending/valuesource/birthdayparty.cpp | 14 +- .../extending/valuesource/birthdayparty.h | 25 +- .../declarative/extending/valuesource/example.qml | 6 +- .../extending/valuesource/happybirthday.cpp | 81 ----- .../extending/valuesource/happybirthday.h | 79 ----- .../extending/valuesource/happybirthdaysong.cpp | 81 +++++ .../extending/valuesource/happybirthdaysong.h | 80 +++++ .../declarative/extending/valuesource/main.cpp | 10 +- .../declarative/extending/valuesource/person.h | 33 +- .../extending/valuesource/valuesource.pro | 4 +- 55 files changed, 827 insertions(+), 787 deletions(-) delete mode 100644 examples/declarative/extending/binding/happybirthday.cpp delete mode 100644 examples/declarative/extending/binding/happybirthday.h create mode 100644 examples/declarative/extending/binding/happybirthdaysong.cpp create mode 100644 examples/declarative/extending/binding/happybirthdaysong.h delete mode 100644 examples/declarative/extending/valuesource/happybirthday.cpp delete mode 100644 examples/declarative/extending/valuesource/happybirthday.h create mode 100644 examples/declarative/extending/valuesource/happybirthdaysong.cpp create mode 100644 examples/declarative/extending/valuesource/happybirthdaysong.h diff --git a/doc/src/declarative/extending.qdoc b/doc/src/declarative/extending.qdoc index d0e5f9e..9844804 100644 --- a/doc/src/declarative/extending.qdoc +++ b/doc/src/declarative/extending.qdoc @@ -43,53 +43,86 @@ \page qml-extending.html \title Extending QML in C++ +The QtDeclarative module provides a set of APIs for extending QML through +C++ extensions. You can write extensions to add your own QML types, extend existing +Qt types, or call C/C++ functions that are not accessible from ordinary QML code. + The QML syntax declaratively describes how to construct an in memory object tree. In Qt, QML is mainly used to describe a visual scene graph, but it is not conceptually limited to this: the QML format is an abstract description of any object tree. All the QML element types included in Qt are implemented using -the C++ extension mechanisms describe on this page. Programmers can use these -APIs to add new types that interact with the existing Qt types, or to repurpose -QML for their own independent use. +the C++ extension mechanisms describe on this page. \tableofcontents \section1 Adding Types \target adding-types +QML can easily be extended to support new types. + +Let's create a new QML type called "Person" that has two properties, a name and a +shoe size. We will make it available in a \l {Modules}{module} called "People", with +a module version of 1.0. We want this \c Person type to be usable from QML like this: + \snippet examples/declarative/extending/adding/example.qml 0 -The QML snippet shown above instantiates one \c Person instance and sets -the name and shoeSize properties on it. Everything in QML ultimately comes down -to either instantiating an object instance, or assigning a property a value. -QML relies heavily on Qt's meta object system and can only instantiate classes -that derive from QObject. +To do this, we need a C++ class that encapsulates this \c Person type and its two +properties. Since QML relies heavily on Qt's \l{Meta-Object System}{meta object system}, +this new class must: + +\list +\o inherit from QObject +\o declare its properties using the Q_PROPERTY() macro +\endlist -The QML engine has no intrinsic knowledge of any class types. Instead the -programmer must define the C++ types, and their corresponding QML name. +Here is the \c Person class: -Custom C++ types are registered using a template function: +\snippet examples/declarative/extending/adding/person.h 0 -\quotation +Now that the \c Person type is defined, it needs to be registered with QML +to make it available to QML code. To do this, call the qmlRegisterType() +template function. For example, this registers the \c Person type as a type called +"Person", to a module named "People", with a module version of 1.0: + +\snippet examples/declarative/extending/adding/main.cpp 0 + +Types can be registered by libraries (as Qt does), application code, +or by plugins (see QDeclarativeExtensionPlugin). + +Now the \c Person type can be used from QML as shown above. When we create a +\c Person item in QML, an instance of the C++ \c Person class is created, and the +\c name and \c shoeSize properties of the \c Person class can be set. All of +the properties of a registered type that are declared with Q_PROPERTY can be used +from QML. + +QML is typesafe. Attempting to assign an invalid value to a property will +generate an error. For example, assuming the name property of the \c Person +element had a type of QString, this would cause an error: \code -template -int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName) +Person { + // Will NOT work + name: 12 +} \endcode -Calling qmlRegisterType() registers the C++ type \a T with the QML system, and makes it available in QML -under the name \a qmlName in library \a uri version \a versionMajor.versionMinor. -The \a qmlName can be the same as the C++ type name. +See \l {Extending QML - Adding Types Example} for the complete code used to create +the \c Person type. + +\section2 Interfaces + +QML also supports assigning Qt interfaces. To assign to a property whose type +is a Qt interface pointer, the interface must also be registered with QML. As +they cannot be instantiated directly, registering a Qt interface is different +from registering a new QML type. Instead of registering the type with +qmlRegisterType(), use qmlRegisterInterface(). -Type \a T must be a concrete type that inherits QObject and has a default -constructor. -\endquotation -Types can be registered by libraries (such as Qt does), application code, -or by plugins (see QDeclarativeExtensionPlugin). +\section1 Supporting object and list property types -Once registered, all of the \l {Qt's Property System}{properties} of a supported -type are available for use within QML. QML has intrinsic support for properties -of these types: +When a new type is registered with QML, all of its properties declared with +Q_PROPERTY are available from QML. QML has intrinsic support for properties of +these types: \list \o bool @@ -105,60 +138,36 @@ of these types: \o QVariant \endlist -QML is typesafe. Attempting to assign an invalid value to a property will -generate an error. For example, assuming the name property of the \c Person -element had a type of QString, this would cause an error: +The \c Person type in the previous example had basic property types - a string +for \c name, and an integer for \c shoeSize. However, QML can set properties of +types that are more complex than basic intrinsics like +integers and strings. Properties can also be object pointers, Qt interface +pointers, lists of object pointers, and lists of Qt interface pointers. -\code -Person { - // Will NOT work - name: 12 -} -\endcode +Let's create a new QML type, "BirthdayParty", that has two properties +with more complex types: -\l {Extending QML - Adding Types Example} shows the complete code used to create -the \c Person type. +\list +\o A \c host property whose value is a \c Person item +\o A \c guests property whose value is a list of \c Person items +\endlist -\section1 Object and List Property Types +We want to use it from QML like this: \snippet examples/declarative/extending/properties/example.qml 0 -The QML snippet shown above assigns a \c Person object to the \c BirthdayParty's -celebrant property, and assigns three \c Person objects to the guests property. - -QML can set properties of types that are more complex than basic intrinsics like -integers and strings. Properties can also be object pointers, Qt interface -pointers, lists of object points, and lists of Qt interface pointers. As QML -is typesafe it ensures that only valid types are assigned to these properties, -just like it does for primitive types. +This will assign a \c Person object to the \c BirthdayParty's \c host property, +and assigns three other \c Person objects in a list to the \c guests property. -Properties that are pointers to objects or Qt interfaces are declared with the -Q_PROPERTY() macro, just like other properties. The celebrant property -declaration looks like this: +To do this, we define a \c BirthdayParty class with the two properties. +The \c host property stores a \c Person* pointer. It is declared like this: \snippet examples/declarative/extending/properties/birthdayparty.h 1 -As long as the property type, in this case Person, is registered with QML the -property can be assigned. - -QML also supports assigning Qt interfaces. To assign to a property whose type -is a Qt interface pointer, the interface must also be registered with QML. As -they cannot be instantiated directly, registering a Qt interface is different -from registering a new QML type. The following function is used instead: - -\quotation -\code -template -int qmlRegisterInterface(const char *typeName) -\endcode - -Registers the C++ interface \a T with the QML system as \a typeName. - -Following registration, QML can coerce objects that implement this interface -for assignment to appropriately typed properties. -\endquotation +Note the type of the property (\c Person in this case) must be registered with QML with +qmlRegisterType() or qmlRegisterInterface(). Otherwise, the property cannot be assigned in QML. -The guests property is a list of \c Person objects. Properties that are lists +The \c guests property is a list of \c Person* objects. Properties that are lists of objects or Qt interfaces are also declared with the Q_PROPERTY() macro, just like other properties. List properties must have the type \c {QDeclarativeListProperty}. As with object properties, the type \a T must be registered with QML. @@ -167,43 +176,44 @@ The guest property declaration looks like this: \snippet examples/declarative/extending/properties/birthdayparty.h 2 +So, here is the complete \c BirthdayParty class declaration: + +\snippet examples/declarative/extending/properties/birthdayparty.h 0 +\snippet examples/declarative/extending/properties/birthdayparty.h 1 +\snippet examples/declarative/extending/properties/birthdayparty.h 2 +\snippet examples/declarative/extending/properties/birthdayparty.h 3 + +The \c BirthdayClass type is also registered using qmlRegisterType() so +it can be used from QML. + \l {Extending QML - Object and List Property Types Example} shows the complete code used to create the \c BirthdayParty type. \section1 Inheritance and Coercion -\snippet examples/declarative/extending/coercion/example.qml 0 - -The QML snippet shown above assigns a \c Boy object to the \c BirthdayParty's -celebrant property, and assigns three other objects to the guests property. - QML supports C++ inheritance heirarchies and can freely coerce between known, valid object types. This enables the creation of common base classes that allow -the assignment of specialized classes to object or list properties. In the -snippet shown, both the celebrant and the guests properties retain the Person -type used in the previous section, but the assignment is valid as both the Boy -and Girl objects inherit from Person. +the assignment of specialized classes to object or list properties. -To assign to a property, the property's type must have been registered with QML. -Both the qmlRegisterType() and qmlRegisterInterface() template functions already -shown can be used to register a type with QML. Additionally, if a type that acts purely -as a base class that cannot be instantiated from QML needs to be -registered, the following function can be used: +For example, if classes \c Girl and \c Boy both inherit from \c Person, then +\c Girl and \c Boy objects can be assigned in place of \c Person objects, like this: + +\snippet examples/declarative/extending/coercion/example.qml 0 + +To assign to a property, the property's type (in this case, \c Girl and \c Boy) +must be registered with QML. +Both the qmlRegisterType() and qmlRegisterInterface() template functions previously +discussed can be used to register a type with QML. Additionally, in the case of the +\c Person class, which now acts purely as a base class and should no longer be instantiated from QML, +the following function can be used: -\quotation \code template int qmlRegisterType() \endcode -Registers the C++ type \a T with the QML system. The parameterless call to the template -function qmlRegisterType() does not define a mapping between the -C++ class and a QML element name, so the type is not instantiable from QML, but -it is available for type coercion. - -Type \a T must inherit QObject, but there are no restrictions on whether it is -concrete or the signature of its constructor. -\endquotation +Calling the parameterless qmlRegisterType() ensures the type is available +to QML for type coercion but cannot be created as a type from QML. QML will automatically coerce C++ types when assigning to either an object property, or to a list property. Only if coercion fails does an assignment @@ -214,71 +224,70 @@ code used to create the \c Boy and \c Girl types. \section1 Default Property -\snippet examples/declarative/extending/default/example.qml 0 +The \e {default property} is a syntactic convenience that allows a type designer to +specify a single property as the type's default. -The QML snippet shown above assigns a collection of objects to the -\c BirthdayParty's default property. +This QML snippet assigns a collection of objects - two \c Boy and one +\c Girl objects - to the \c BirthdayParty's default property: -The default property is a syntactic convenience that allows a type designer to -specify a single property as the type's default. The default property is -assigned to whenever no explicit property is specified. As a convenience, it is -behaviorally identical to assigning the default property explicitly by name. +\snippet examples/declarative/extending/default/example.qml 0 + +The default property is assigned to whenever no explicit property is specified. +As a convenience, it is behaviorally identical to assigning the default property +explicitly by name. From C++, type designers mark the default property using a Q_CLASSINFO() annotation: -\quotation \code -Q_CLASSINFO("DefaultProperty", "property") + Q_CLASSINFO("DefaultProperty", "property") \endcode -Mark \a property as the class's default property. \a property must be either -an object property, or a list property. +The \c property must be either an object property, or a list property. The +\c property can refer to a property declared in the class itself, or a property +inherited from a base class. A default property is optional. A derived class inherits its base class's -default property, but may override it in its own declaration. \a property can -refer to a property declared in the class itself, or a property inherited from a -base class. -\endquotation +default property, but may override it in its own declaration. \l {Extending QML - Default Property Example} shows the complete code used to specify a default property. -\section1 Grouped Properties - -\snippet examples/declarative/extending/grouped/example.qml 1 -The QML snippet shown above assigns a number properties to the \c Boy object, -including four properties using the grouped property syntax. +\section1 Grouped Properties Grouped properties collect similar properties together into a single named block. Grouped properties can be used to present a nicer API to developers, and may also simplify the implementation of common property collections across different types through implementation reuse. +This QML snippet assigns a number properties to the \c Boy object, +including four properties using the grouped property syntax: + +\snippet examples/declarative/extending/grouped/example.qml 1 + A grouped property block is implemented as a read-only object property. The shoe property shown is declared like this: \snippet examples/declarative/extending/grouped/person.h 1 -The ShoeDescription type declares the properties available to the grouped -property block - in this case the size, color, brand and price properties. +The \c ShoeDescription type declares the properties available to the grouped +property block - in this case the \c size, \c color, \c brand and \c price properties. -Grouped property blocks may declared and accessed be recusively. +Grouped property blocks may be declared and accessed recusively. \l {Extending QML - Grouped Properties Example} shows the complete code used to implement the \c shoe property grouping. \section1 Attached Properties -\snippet examples/declarative/extending/attached/example.qml 1 +Attached properties allow unrelated types to annotate other types with some +additional properties, generally for their own use. This QML snippet assigns +an \c rsvp property using the attached property syntax: -The QML snippet shown above assigns the rsvp property using the attached -property syntax. +\snippet examples/declarative/extending/attached/example.qml 1 -Attached properties allow unrelated types to annotate other types with some -additional properties, generally for their own use. Attached properties are -identified through the use of the attacher type name, in the case shown +Attached properties are identified through the use of the attacher type name; in the case shown \c BirthdayParty, as a suffix to the property name. In the example shown, \c BirthdayParty is called the attaching type, and the @@ -289,35 +298,36 @@ QObject derived type, called the attachment object. The properties on the attachment object are those that become available for use as the attached property block. -Any QML type can become an attaching type by declaring the +Any QML type can become an attaching type by declaring a \c qmlAttachedProperties() public function and declaring that the class has QML_HAS_ATTACHED_PROPERTIES: -\quotation \code -class MyType : public QObject { - Q_OBJECT -public: + class MyType : public QObject { + Q_OBJECT + public: - ... + ... - static AttachedPropertiesType *qmlAttachedProperties(QObject *object); -}; + static AttachedPropertiesType *qmlAttachedProperties(QObject *object); + }; -QML_DECLARE_TYPEINFO(MyType, QML_HAS_ATTACHED_PROPERTIES) + QML_DECLARE_TYPEINFO(MyType, QML_HAS_ATTACHED_PROPERTIES) \endcode -Return an attachment object, of type \a AttachedPropertiesType, for the + +The qmlAttachedProperties() method returns an attachment object, of type \a AttachedPropertiesType, for the attachee \a object instance. It is customary, though not strictly required, for the attachment object to be parented to \a object to prevent memory leaks. -\a AttachedPropertiesType must be a QObject derived type. The properties on +The returned \a AttachedPropertiesType must be a QObject derived type. The properties on this type will be accessible through the attached properties syntax. -This method will be called at most once for each attachee object instance. The +This method is called at most once for each attachee object instance. The QML engine will cache the returned instance pointer for subsequent attached property accesses. Consequently the attachment object may not be deleted until \a object is destroyed. -\endquotation + +\section2 Use of attached properties Conceptually, attached properties are a \e type exporting a set of additional properties that can be set on \e any other object instance. Attached properties @@ -326,15 +336,18 @@ their effect may be so limited. For example, a common usage scenario is for a type to enhance the properties available to its children in order to gather instance specific data. Here we -add a rsvp field to all the guests coming to a birthday party: +add a \c rsvp field to all the guests coming to a birthday party: + \code BirthdayParty { Boy { BirthdayParty.rsvp: "2009-06-01" } } \endcode + However, as a type cannot limit the instances to which the attachment object must attach, the following is also allowed, even though adding a birthday party rsvp in this context will have no effect. + \code GraduationParty { Boy { BirthdayParty.rsvp: "2009-06-01" } @@ -362,7 +375,7 @@ implement the rsvp attached property. \section1 Memory Management and QVariant types -It is an elements responsibility to ensure that it does not access or return +It is an element's responsibility to ensure that it does not access or return pointers to invalid objects. QML makes the following guarentees: \list @@ -393,21 +406,24 @@ this situation, but it must not crash. \section1 Signal Support -\snippet examples/declarative/extending/signal/example.qml 0 -\snippet examples/declarative/extending/signal/example.qml 1 - -The QML snippet shown above associates the evaluation of a JavaScript expression -with the emission of a Qt signal. - All Qt signals on a registered class become available as special "signal properties" within QML to which the user can assign a single JavaScript expression. The signal property's name is a transformed version of the Qt signal name: "on" is prepended, and the first letter of the signal name upper -cased. For example, the signal used in the example above has the following -C++ signature: +cased. + +For example, if the \c BirthdayParty class has a signal like this: \snippet examples/declarative/extending/signal/birthdayparty.h 0 +This signal can be connected to from QML like this: + +\snippet examples/declarative/extending/signal/example.qml 0 +\snippet examples/declarative/extending/signal/example.qml 1 + +This associates the evaluation of a JavaScript expression +with the emission of a Qt signal. + In classes with multiple signals with the same name, only the final signal is accessible as a signal property. Note that signals with the same name but different parameters cannot be distinguished. @@ -424,38 +440,35 @@ implement the onPartyStarted signal property. \section1 Property Value Sources -\snippet examples/declarative/extending/valuesource/example.qml 0 -\snippet examples/declarative/extending/valuesource/example.qml 1 - -The QML snippet shown above applies a property value source to the speaker property. -A property value source generates a value for a property that changes over time. +A property value source is an element that generates a value for a +property that changes over time. Property value sources are most commonly used to +create animations; QML's built-in \l {Behavior}{Behavior} and various +\l {QML Animation}{animation} elements are actually property value sources. -Property value sources are most commonly used to do animation. Rather than -constructing an animation object and manually setting the animation's "target" -property, a property value source can be assigned directly to a property of any -type and automatically set up this association. +The \c BirthdayParty class has an \c announcement string property +whose value is printed to the console whenever it changes. Using a property value +source called \c HappyBirthdaySong, the value of this property can be changed, +over time, to each lyric in the song "Happy Birthday". For further customization, +a \c name property allows the song lyrics to be personalized with a particular name. +The \c HappyBirthdaySong property value source would be used in QML like this: -The example shown here is rather contrived: the speaker property of the -BirthdayParty object is a string that is printed every time it is assigned and -the HappyBirthday value source generates the lyrics of the song -"Happy Birthday". - -\snippet examples/declarative/extending/valuesource/birthdayparty.h 0 +\snippet examples/declarative/extending/valuesource/example.qml 0 +\snippet examples/declarative/extending/valuesource/example.qml 1 -Normally, assigning an object to a string property would not be allowed. In +(Normally, assigning an object to a string property would not be allowed. In the case of a property value source, rather than assigning the object instance itself, the QML engine sets up an association between the value source and -the property. +the property.) Property value sources are special types that derive from the QDeclarativePropertyValueSource base class. This base class contains a single method, QDeclarativePropertyValueSource::setTarget(), that the QML engine invokes when -associating the property value source with a property. The relevant part of -the HappyBirthday type declaration looks like this: +associating the property value source with a property. Here is the relevant part of +the \c HappyBirthdaySong type declaration: -\snippet examples/declarative/extending/valuesource/happybirthday.h 0 -\snippet examples/declarative/extending/valuesource/happybirthday.h 1 -\snippet examples/declarative/extending/valuesource/happybirthday.h 2 +\snippet examples/declarative/extending/valuesource/happybirthdaysong.h 0 +\snippet examples/declarative/extending/valuesource/happybirthdaysong.h 1 +\snippet examples/declarative/extending/valuesource/happybirthdaysong.h 2 In all other respects, property value sources are regular QML types. They must be registered with the QML engine using the same macros as other types, and can @@ -463,20 +476,15 @@ contain properties, signals and methods just like other types. When a property value source object is assigned to a property, QML first tries to assign it normally, as though it were a regular QML type. Only if this -assignment fails does the engine call the setTarget() method. This allows +assignment fails does the engine call the +\l {QDeclarativePropertyValueSource::setTarget()}{setTarget()} method. This allows the type to also be used in contexts other than just as a value source. \l {Extending QML - Property Value Source Example} shows the complete code used -implement the HappyBirthday property value source. +implement the HappyBirthdaySong property value source. \section1 Property Binding -\snippet examples/declarative/extending/binding/example.qml 0 -\snippet examples/declarative/extending/binding/example.qml 1 - -The QML snippet shown above uses a property binding to ensure the -HappyBirthday's name property remains up to date with the celebrant. - Property binding is a core feature of QML. In addition to assigning literal values, property bindings allow the developer to assign an arbitrarily complex JavaScript expression that may include dependencies on other property values. @@ -488,15 +496,23 @@ All properties on custom types automatically support property binding. However, for binding to work correctly, QML must be able to reliably determine when a property has changed so that it knows to reevaluate any bindings that depend on the property's value. QML relies on the presence of a -\c {Qt's Property System}{NOTIFY signal} for this determination. +\l {Qt's Property System}{NOTIFY signal} for this determination. -Here is the celebrant property declaration: +For example, to enable property binding for the \c host property in \c BirthdayParty, +add a \c hostChanged signal that is emitted when the \c host value changes, and +declare the \c host property like this: \snippet examples/declarative/extending/binding/birthdayparty.h 0 -The NOTIFY attribute is followed by a signal name. It is the responsibility of -the class implementer to ensure that whenever the property's value changes, the -NOTIFY signal is emitted. The signature of the NOTIFY signal is not important to QML. +This enables the use of a property binding to ensure the \c name value of the +\c HappyBirthdaySong is updated if the \c host value changes: + +\snippet examples/declarative/extending/binding/example.qml 0 +\snippet examples/declarative/extending/binding/example.qml 1 + +It is the responsibility of the class implementer to ensure that whenever the +property's value changes, the associated NOTIFY signal is emitted. The +signature of the NOTIFY signal is not important to QML. To prevent loops or excessive evaluation, developers should ensure that the signal is only emitted whenever the property's value is actually changed. If @@ -504,6 +520,8 @@ a property, or group of properties, is infrequently used it is permitted to use the same NOTIFY signal for several properties. This should be done with care to ensure that performance doesn't suffer. +\section2 Use of notify signals + To keep QML reliable, if a property does not have a NOTIFY signal, it cannot be used in a binding expression. However, the property can still be assigned a binding as QML does not need to monitor the property for change in that @@ -543,11 +561,6 @@ include NOTIFY signals for use in binding. \section1 Extension Objects -\snippet examples/declarative/extending/extended/example.qml 0 - -The QML snippet shown above adds a new property to an existing C++ type without -modifying its source code. - When integrating existing classes and technology into QML, their APIs will often need to be tweaked to fit better into the declarative environment. Although the best results are usually obtained by modifying the original classes @@ -561,6 +574,11 @@ type definition allows the programmer to supply an additional type - known as th extension type - when registering the target class whose properties are transparently merged with the original target class when used from within QML. +Here is a QML snippet that adds a new property to an existing C++ type without +modifying its source code: + +\snippet examples/declarative/extending/extended/example.qml 0 + An extension class is a regular QObject, with a constructor that takes a QObject pointer. When needed (extension classes are delay created until the first extended property is accessed) the extension class is created and the target object is @@ -572,6 +590,7 @@ When an extended type is installed, one of the #define QML_REGISTER_EXTENDED_TYPE(URI, VMAJ, VFROM, VTO, QDeclarativeName,T, ExtendedT) #define QML_REGISTER_EXTENDED_NOCREATE_TYPE(T, ExtendedT) \endcode + macros should be used instead of the regular \c QML_REGISTER_TYPE or \c QML_REGISTER_NOCREATE_TYPE. The arguments are identical to the corresponding non-extension object macro, except for the ExtendedT parameter which is the type diff --git a/examples/declarative/extending/adding/example.qml b/examples/declarative/extending/adding/example.qml index c608f94..dc891e7 100644 --- a/examples/declarative/extending/adding/example.qml +++ b/examples/declarative/extending/adding/example.qml @@ -1,6 +1,6 @@ +// ![0] import People 1.0 -// ![0] Person { name: "Bob Jones" shoeSize: 12 diff --git a/examples/declarative/extending/adding/main.cpp b/examples/declarative/extending/adding/main.cpp index b9e5aa3..7b33895 100644 --- a/examples/declarative/extending/adding/main.cpp +++ b/examples/declarative/extending/adding/main.cpp @@ -47,8 +47,9 @@ int main(int argc, char ** argv) { QCoreApplication app(argc, argv); - +//![0] qmlRegisterType("People", 1,0, "Person"); +//![0] QDeclarativeEngine engine; QDeclarativeComponent component(&engine, ":example.qml"); diff --git a/examples/declarative/extending/adding/person.h b/examples/declarative/extending/adding/person.h index 7a9e0f0..d6de9a9 100644 --- a/examples/declarative/extending/adding/person.h +++ b/examples/declarative/extending/adding/person.h @@ -43,12 +43,11 @@ #include // ![0] -#include - -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) public: Person(QObject *parent = 0); @@ -57,6 +56,7 @@ public: int shoeSize() const; void setShoeSize(int); + private: QString m_name; int m_shoeSize; diff --git a/examples/declarative/extending/attached/birthdayparty.cpp b/examples/declarative/extending/attached/birthdayparty.cpp index d4f2675..7fa1748 100644 --- a/examples/declarative/extending/attached/birthdayparty.cpp +++ b/examples/declarative/extending/attached/birthdayparty.cpp @@ -56,18 +56,18 @@ void BirthdayPartyAttached::setRsvp(const QDate &d) } BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/attached/birthdayparty.h b/examples/declarative/extending/attached/birthdayparty.h index 7c45d21..1c66f8c 100644 --- a/examples/declarative/extending/attached/birthdayparty.h +++ b/examples/declarative/extending/attached/birthdayparty.h @@ -48,8 +48,8 @@ class BirthdayPartyAttached : public QObject { -Q_OBJECT -Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) + Q_OBJECT + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) public: BirthdayPartyAttached(QObject *object); @@ -62,15 +62,15 @@ private: class BirthdayParty : public QObject { -Q_OBJECT -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) -Q_CLASSINFO("DefaultProperty", "guests") + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; @@ -78,7 +78,7 @@ public: static BirthdayPartyAttached *qmlAttachedProperties(QObject *); private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; diff --git a/examples/declarative/extending/attached/example.qml b/examples/declarative/extending/attached/example.qml index 952eb93..50f0a32 100644 --- a/examples/declarative/extending/attached/example.qml +++ b/examples/declarative/extending/attached/example.qml @@ -1,16 +1,17 @@ import People 1.0 BirthdayParty { - celebrant: Boy { + host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } // ![1] Boy { - name: "Joan Hodges" - BirthdayParty.rsvp: "2009-07-06" + name: "Leo Hodges" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + + BirthdayParty.rsvp: "2009-07-06" } // ![1] Boy { @@ -19,11 +20,12 @@ BirthdayParty { } Girl { name: "Anne Brown" - BirthdayParty.rsvp: "2009-07-01" shoe.size: 7 shoe.color: "red" shoe.brand: "Marc Jacobs" shoe.price: 699.99 + + BirthdayParty.rsvp: "2009-07-01" } } diff --git a/examples/declarative/extending/attached/main.cpp b/examples/declarative/extending/attached/main.cpp index fd2d525..f1055ae 100644 --- a/examples/declarative/extending/attached/main.cpp +++ b/examples/declarative/extending/attached/main.cpp @@ -60,10 +60,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/attached/person.h b/examples/declarative/extending/attached/person.h index 7a4b9c3..2f444c5 100644 --- a/examples/declarative/extending/attached/person.h +++ b/examples/declarative/extending/attached/person.h @@ -43,14 +43,14 @@ #include #include -#include -class ShoeDescription : public QObject { -Q_OBJECT -Q_PROPERTY(int size READ size WRITE setSize) -Q_PROPERTY(QColor color READ color WRITE setColor) -Q_PROPERTY(QString brand READ brand WRITE setBrand) -Q_PROPERTY(qreal price READ price WRITE setPrice) +class ShoeDescription : public QObject +{ + Q_OBJECT + Q_PROPERTY(int size READ size WRITE setSize) + Q_PROPERTY(QColor color READ color WRITE setColor) + Q_PROPERTY(QString brand READ brand WRITE setBrand) + Q_PROPERTY(qreal price READ price WRITE setPrice) public: ShoeDescription(QObject *parent = 0); @@ -72,10 +72,11 @@ private: qreal m_price; }; -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(ShoeDescription *shoe READ shoe) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(ShoeDescription *shoe READ shoe) public: Person(QObject *parent = 0); @@ -88,14 +89,16 @@ private: ShoeDescription m_shoe; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/binding/binding.pro b/examples/declarative/extending/binding/binding.pro index 903712e..896ce25 100644 --- a/examples/declarative/extending/binding/binding.pro +++ b/examples/declarative/extending/binding/binding.pro @@ -8,10 +8,10 @@ QT += declarative SOURCES += main.cpp \ person.cpp \ birthdayparty.cpp \ - happybirthday.cpp + happybirthdaysong.cpp HEADERS += person.h \ birthdayparty.h \ - happybirthday.h + happybirthdaysong.h RESOURCES += binding.qrc target.path = $$[QT_INSTALL_EXAMPLES]/declarative/extending/binding sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS binding.pro diff --git a/examples/declarative/extending/binding/birthdayparty.cpp b/examples/declarative/extending/binding/birthdayparty.cpp index e5be2b9..000bb1f 100644 --- a/examples/declarative/extending/binding/birthdayparty.cpp +++ b/examples/declarative/extending/binding/birthdayparty.cpp @@ -60,20 +60,20 @@ void BirthdayPartyAttached::setRsvp(const QDate &d) BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - if (c == m_celebrant) return; - m_celebrant = c; - emit celebrantChanged(); + if (c == m_host) return; + m_host = c; + emit hostChanged(); } QDeclarativeListProperty BirthdayParty::guests() @@ -97,12 +97,12 @@ void BirthdayParty::startParty() emit partyStarted(time); } -QString BirthdayParty::speaker() const +QString BirthdayParty::announcement() const { return QString(); } -void BirthdayParty::setSpeaker(const QString &speak) +void BirthdayParty::setAnnouncement(const QString &speak) { qWarning() << qPrintable(speak); } diff --git a/examples/declarative/extending/binding/birthdayparty.h b/examples/declarative/extending/binding/birthdayparty.h index e2757bc..c3f033e 100644 --- a/examples/declarative/extending/binding/birthdayparty.h +++ b/examples/declarative/extending/binding/birthdayparty.h @@ -49,8 +49,8 @@ class BirthdayPartyAttached : public QObject { -Q_OBJECT -Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged) + Q_OBJECT + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged) public: BirthdayPartyAttached(QObject *object); @@ -66,35 +66,35 @@ private: class BirthdayParty : public QObject { -Q_OBJECT + Q_OBJECT // ![0] -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant NOTIFY celebrantChanged) + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged) // ![0] -Q_PROPERTY(QDeclarativeListProperty guests READ guests) -Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) -Q_CLASSINFO("DefaultProperty", "guests") + Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement) + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; - QString speaker() const; - void setSpeaker(const QString &); + QString announcement() const; + void setAnnouncement(const QString &); static BirthdayPartyAttached *qmlAttachedProperties(QObject *); void startParty(); signals: void partyStarted(const QTime &time); - void celebrantChanged(); + void hostChanged(); private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; diff --git a/examples/declarative/extending/binding/example.qml b/examples/declarative/extending/binding/example.qml index b67049b..82eb3be 100644 --- a/examples/declarative/extending/binding/example.qml +++ b/examples/declarative/extending/binding/example.qml @@ -4,9 +4,9 @@ import People 1.0 BirthdayParty { id: theParty - HappyBirthday on speaker { name: theParty.celebrant.name } + HappyBirthdaySong on announcement { name: theParty.host.name } - celebrant: Boy { + host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } @@ -15,7 +15,7 @@ BirthdayParty { Boy { - name: "Joan Hodges" + name: "Leo Hodges" BirthdayParty.rsvp: "2009-07-06" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } diff --git a/examples/declarative/extending/binding/happybirthday.cpp b/examples/declarative/extending/binding/happybirthday.cpp deleted file mode 100644 index aa5f937..0000000 --- a/examples/declarative/extending/binding/happybirthday.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "happybirthday.h" -#include - -HappyBirthday::HappyBirthday(QObject *parent) -: QObject(parent), m_line(-1) -{ - setName(QString()); - QTimer *timer = new QTimer(this); - QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); - timer->start(1000); -} - -void HappyBirthday::setTarget(const QDeclarativeProperty &p) -{ - m_target = p; -} - -QString HappyBirthday::name() const -{ - return m_name; -} - -void HappyBirthday::setName(const QString &name) -{ - if (m_name == name) - return; - - m_name = name; - - m_lyrics.clear(); - m_lyrics << "Happy birthday to you,"; - m_lyrics << "Happy birthday to you,"; - m_lyrics << "Happy birthday dear " + m_name + ","; - m_lyrics << "Happy birthday to you!"; - m_lyrics << ""; - - emit nameChanged(); -} - -void HappyBirthday::advance() -{ - m_line = (m_line + 1) % m_lyrics.count(); - - m_target.write(m_lyrics.at(m_line)); -} - diff --git a/examples/declarative/extending/binding/happybirthday.h b/examples/declarative/extending/binding/happybirthday.h deleted file mode 100644 index 0e5a90a..0000000 --- a/examples/declarative/extending/binding/happybirthday.h +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef HAPPYBIRTHDAY_H -#define HAPPYBIRTHDAY_H - -#include -#include -#include - -#include - -class HappyBirthday : public QObject, public QDeclarativePropertyValueSource -{ -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) -Q_INTERFACES(QDeclarativePropertyValueSource) -public: - HappyBirthday(QObject *parent = 0); - - virtual void setTarget(const QDeclarativeProperty &); - - QString name() const; - void setName(const QString &); - -private slots: - void advance(); - -signals: - void nameChanged(); -private: - int m_line; - QStringList m_lyrics; - QDeclarativeProperty m_target; - QString m_name; -}; - -#endif // HAPPYBIRTHDAY_H - diff --git a/examples/declarative/extending/binding/happybirthdaysong.cpp b/examples/declarative/extending/binding/happybirthdaysong.cpp new file mode 100644 index 0000000..a40e7fb --- /dev/null +++ b/examples/declarative/extending/binding/happybirthdaysong.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "happybirthdaysong.h" +#include + +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) +: QObject(parent), m_line(-1) +{ + setName(QString()); + QTimer *timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); + timer->start(1000); +} + +void HappyBirthdaySong::setTarget(const QDeclarativeProperty &p) +{ + m_target = p; +} + +QString HappyBirthdaySong::name() const +{ + return m_name; +} + +void HappyBirthdaySong::setName(const QString &name) +{ + if (m_name == name) + return; + + m_name = name; + + m_lyrics.clear(); + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday dear " + m_name + ","; + m_lyrics << "Happy birthday to you!"; + m_lyrics << ""; + + emit nameChanged(); +} + +void HappyBirthdaySong::advance() +{ + m_line = (m_line + 1) % m_lyrics.count(); + + m_target.write(m_lyrics.at(m_line)); +} + diff --git a/examples/declarative/extending/binding/happybirthdaysong.h b/examples/declarative/extending/binding/happybirthdaysong.h new file mode 100644 index 0000000..e825b86 --- /dev/null +++ b/examples/declarative/extending/binding/happybirthdaysong.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef HAPPYBIRTHDAYSONG_H +#define HAPPYBIRTHDAYSONG_H + +#include +#include + +#include + +class HappyBirthdaySong : public QObject, public QDeclarativePropertyValueSource +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_INTERFACES(QDeclarativePropertyValueSource) +public: + HappyBirthdaySong(QObject *parent = 0); + + virtual void setTarget(const QDeclarativeProperty &); + + QString name() const; + void setName(const QString &); + +private slots: + void advance(); + +signals: + void nameChanged(); +private: + int m_line; + QStringList m_lyrics; + QDeclarativeProperty m_target; + QString m_name; +}; + +#endif // HAPPYBIRTHDAYSONG_H + diff --git a/examples/declarative/extending/binding/main.cpp b/examples/declarative/extending/binding/main.cpp index ce6c50d..2495676 100644 --- a/examples/declarative/extending/binding/main.cpp +++ b/examples/declarative/extending/binding/main.cpp @@ -43,7 +43,7 @@ #include #include #include "birthdayparty.h" -#include "happybirthday.h" +#include "happybirthdaysong.h" #include "person.h" int main(int argc, char ** argv) @@ -51,7 +51,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); qmlRegisterType(); qmlRegisterType("People", 1,0, "BirthdayParty"); - qmlRegisterType("People", 1,0, "HappyBirthday"); + qmlRegisterType("People", 1,0, "HappyBirthdaySong"); qmlRegisterType(); qmlRegisterType(); qmlRegisterType("People", 1,0, "Boy"); @@ -61,10 +61,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/binding/person.h b/examples/declarative/extending/binding/person.h index 0edfcdd..2a68da0 100644 --- a/examples/declarative/extending/binding/person.h +++ b/examples/declarative/extending/binding/person.h @@ -43,14 +43,14 @@ #include #include -#include -class ShoeDescription : public QObject { -Q_OBJECT -Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged) -Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged) -Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged) -Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged) +class ShoeDescription : public QObject +{ + Q_OBJECT + Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged) + Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged) + Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged) public: ShoeDescription(QObject *parent = 0); @@ -75,11 +75,12 @@ private: qreal m_price; }; -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) // ![0] -Q_PROPERTY(ShoeDescription *shoe READ shoe CONSTANT) + Q_PROPERTY(ShoeDescription *shoe READ shoe CONSTANT) // ![0] public: Person(QObject *parent = 0); @@ -96,14 +97,16 @@ private: ShoeDescription m_shoe; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/coercion/birthdayparty.cpp b/examples/declarative/extending/coercion/birthdayparty.cpp index 523a42d..4f415a3 100644 --- a/examples/declarative/extending/coercion/birthdayparty.cpp +++ b/examples/declarative/extending/coercion/birthdayparty.cpp @@ -41,18 +41,18 @@ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/coercion/birthdayparty.h b/examples/declarative/extending/coercion/birthdayparty.h index a5d14f9..ee77e9a 100644 --- a/examples/declarative/extending/coercion/birthdayparty.h +++ b/examples/declarative/extending/coercion/birthdayparty.h @@ -42,28 +42,28 @@ #define BIRTHDAYPARTY_H #include -#include +#include #include "person.h" class BirthdayParty : public QObject { -Q_OBJECT + Q_OBJECT // ![0] -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) // ![0] public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; diff --git a/examples/declarative/extending/coercion/example.qml b/examples/declarative/extending/coercion/example.qml index 64d26b0..7b45950 100644 --- a/examples/declarative/extending/coercion/example.qml +++ b/examples/declarative/extending/coercion/example.qml @@ -2,12 +2,12 @@ import People 1.0 // ![0] BirthdayParty { - celebrant: Boy { + host: Boy { name: "Bob Jones" shoeSize: 12 } guests: [ - Boy { name: "Joan Hodges" }, + Boy { name: "Leo Hodges" }, Boy { name: "Jack Smith" }, Girl { name: "Anne Brown" } ] diff --git a/examples/declarative/extending/coercion/main.cpp b/examples/declarative/extending/coercion/main.cpp index 312aff9..2c7b545 100644 --- a/examples/declarative/extending/coercion/main.cpp +++ b/examples/declarative/extending/coercion/main.cpp @@ -60,10 +60,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/coercion/person.h b/examples/declarative/extending/coercion/person.h index 861f135..1c95da7 100644 --- a/examples/declarative/extending/coercion/person.h +++ b/examples/declarative/extending/coercion/person.h @@ -42,12 +42,12 @@ #define PERSON_H #include -#include -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) public: Person(QObject *parent = 0); @@ -63,15 +63,17 @@ private: // ![0] -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/default/birthdayparty.cpp b/examples/declarative/extending/default/birthdayparty.cpp index 523a42d..4f415a3 100644 --- a/examples/declarative/extending/default/birthdayparty.cpp +++ b/examples/declarative/extending/default/birthdayparty.cpp @@ -41,18 +41,18 @@ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/default/birthdayparty.h b/examples/declarative/extending/default/birthdayparty.h index c0cb0a4..9741040 100644 --- a/examples/declarative/extending/default/birthdayparty.h +++ b/examples/declarative/extending/default/birthdayparty.h @@ -42,28 +42,28 @@ #define BIRTHDAYPARTY_H #include -#include +#include #include "person.h" // ![0] class BirthdayParty : public QObject { -Q_OBJECT -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) -Q_CLASSINFO("DefaultProperty", "guests") + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; // ![0] diff --git a/examples/declarative/extending/default/example.qml b/examples/declarative/extending/default/example.qml index 58035f9..c0f3cbb 100644 --- a/examples/declarative/extending/default/example.qml +++ b/examples/declarative/extending/default/example.qml @@ -2,12 +2,12 @@ import People 1.0 // ![0] BirthdayParty { - celebrant: Boy { + host: Boy { name: "Bob Jones" shoeSize: 12 } - Boy { name: "Joan Hodges" } + Boy { name: "Leo Hodges" } Boy { name: "Jack Smith" } Girl { name: "Anne Brown" } } diff --git a/examples/declarative/extending/default/main.cpp b/examples/declarative/extending/default/main.cpp index 06282ad..2ffd180 100644 --- a/examples/declarative/extending/default/main.cpp +++ b/examples/declarative/extending/default/main.cpp @@ -58,10 +58,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/default/person.h b/examples/declarative/extending/default/person.h index 832bf11..3e56860 100644 --- a/examples/declarative/extending/default/person.h +++ b/examples/declarative/extending/default/person.h @@ -42,12 +42,12 @@ #define PERSON_H #include -#include -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) public: Person(QObject *parent = 0); @@ -61,14 +61,16 @@ private: int m_shoeSize; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/extended/lineedit.h b/examples/declarative/extending/extended/lineedit.h index 9730b91..3a464b0 100644 --- a/examples/declarative/extending/extended/lineedit.h +++ b/examples/declarative/extending/extended/lineedit.h @@ -45,11 +45,11 @@ class LineEditExtension : public QObject { -Q_OBJECT -Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin NOTIFY marginsChanged) -Q_PROPERTY(int rightMargin READ rightMargin WRITE setRightMargin NOTIFY marginsChanged) -Q_PROPERTY(int topMargin READ topMargin WRITE setTopMargin NOTIFY marginsChanged) -Q_PROPERTY(int bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY marginsChanged) + Q_OBJECT + Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin NOTIFY marginsChanged) + Q_PROPERTY(int rightMargin READ rightMargin WRITE setRightMargin NOTIFY marginsChanged) + Q_PROPERTY(int topMargin READ topMargin WRITE setTopMargin NOTIFY marginsChanged) + Q_PROPERTY(int bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY marginsChanged) public: LineEditExtension(QObject *); diff --git a/examples/declarative/extending/grouped/birthdayparty.cpp b/examples/declarative/extending/grouped/birthdayparty.cpp index 523a42d..4f415a3 100644 --- a/examples/declarative/extending/grouped/birthdayparty.cpp +++ b/examples/declarative/extending/grouped/birthdayparty.cpp @@ -41,18 +41,18 @@ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/grouped/birthdayparty.h b/examples/declarative/extending/grouped/birthdayparty.h index 4ac5602..31d21b2 100644 --- a/examples/declarative/extending/grouped/birthdayparty.h +++ b/examples/declarative/extending/grouped/birthdayparty.h @@ -42,27 +42,27 @@ #define BIRTHDAYPARTY_H #include -#include +#include #include "person.h" class BirthdayParty : public QObject { -Q_OBJECT -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) -Q_CLASSINFO("DefaultProperty", "guests") + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; diff --git a/examples/declarative/extending/grouped/example.qml b/examples/declarative/extending/grouped/example.qml index 55912ed..91b7a06 100644 --- a/examples/declarative/extending/grouped/example.qml +++ b/examples/declarative/extending/grouped/example.qml @@ -2,13 +2,13 @@ import People 1.0 // ![0] BirthdayParty { - celebrant: Boy { + host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } Boy { - name: "Joan Hodges" + name: "Leo Hodges" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } // ![1] diff --git a/examples/declarative/extending/grouped/main.cpp b/examples/declarative/extending/grouped/main.cpp index b383a8b..baf32cf 100644 --- a/examples/declarative/extending/grouped/main.cpp +++ b/examples/declarative/extending/grouped/main.cpp @@ -59,10 +59,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/grouped/person.h b/examples/declarative/extending/grouped/person.h index 216c015..a031e69 100644 --- a/examples/declarative/extending/grouped/person.h +++ b/examples/declarative/extending/grouped/person.h @@ -43,14 +43,14 @@ #include #include -#include -class ShoeDescription : public QObject { -Q_OBJECT -Q_PROPERTY(int size READ size WRITE setSize) -Q_PROPERTY(QColor color READ color WRITE setColor) -Q_PROPERTY(QString brand READ brand WRITE setBrand) -Q_PROPERTY(qreal price READ price WRITE setPrice) +class ShoeDescription : public QObject +{ + Q_OBJECT + Q_PROPERTY(int size READ size WRITE setSize) + Q_PROPERTY(QColor color READ color WRITE setColor) + Q_PROPERTY(QString brand READ brand WRITE setBrand) + Q_PROPERTY(qreal price READ price WRITE setPrice) public: ShoeDescription(QObject *parent = 0); @@ -72,11 +72,12 @@ private: qreal m_price; }; -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) // ![1] -Q_PROPERTY(ShoeDescription *shoe READ shoe) + Q_PROPERTY(ShoeDescription *shoe READ shoe) // ![1] public: Person(QObject *parent = 0); @@ -90,14 +91,16 @@ private: ShoeDescription m_shoe; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/properties/birthdayparty.cpp b/examples/declarative/extending/properties/birthdayparty.cpp index 14fd6a3..27d17a1 100644 --- a/examples/declarative/extending/properties/birthdayparty.cpp +++ b/examples/declarative/extending/properties/birthdayparty.cpp @@ -41,19 +41,19 @@ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } // ![0] -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/properties/birthdayparty.h b/examples/declarative/extending/properties/birthdayparty.h index dd01562..39ce9ba 100644 --- a/examples/declarative/extending/properties/birthdayparty.h +++ b/examples/declarative/extending/properties/birthdayparty.h @@ -42,33 +42,33 @@ #define BIRTHDAYPARTY_H #include -#include +#include #include "person.h" // ![0] class BirthdayParty : public QObject { -Q_OBJECT + Q_OBJECT // ![0] // ![1] -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) + Q_PROPERTY(Person *host READ host WRITE setHost) // ![1] // ![2] -Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) // ![2] // ![3] public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; // ![3] diff --git a/examples/declarative/extending/properties/example.qml b/examples/declarative/extending/properties/example.qml index 9594a84..35abdd6 100644 --- a/examples/declarative/extending/properties/example.qml +++ b/examples/declarative/extending/properties/example.qml @@ -2,12 +2,12 @@ import People 1.0 // ![0] BirthdayParty { - celebrant: Person { + host: Person { name: "Bob Jones" shoeSize: 12 } guests: [ - Person { name: "Joan Hodges" }, + Person { name: "Leo Hodges" }, Person { name: "Jack Smith" }, Person { name: "Anne Brown" } ] diff --git a/examples/declarative/extending/properties/main.cpp b/examples/declarative/extending/properties/main.cpp index 350f8bd..85e9584 100644 --- a/examples/declarative/extending/properties/main.cpp +++ b/examples/declarative/extending/properties/main.cpp @@ -56,8 +56,8 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; qWarning() << "They are inviting:"; for (int ii = 0; ii < party->guestCount(); ++ii) qWarning() << " " << party->guest(ii)->name(); diff --git a/examples/declarative/extending/properties/person.h b/examples/declarative/extending/properties/person.h index 7504d18..0029b09 100644 --- a/examples/declarative/extending/properties/person.h +++ b/examples/declarative/extending/properties/person.h @@ -42,12 +42,12 @@ #define PERSON_H #include -#include -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) public: Person(QObject *parent = 0); diff --git a/examples/declarative/extending/signal/birthdayparty.cpp b/examples/declarative/extending/signal/birthdayparty.cpp index 65ff530..740c8c9 100644 --- a/examples/declarative/extending/signal/birthdayparty.cpp +++ b/examples/declarative/extending/signal/birthdayparty.cpp @@ -57,18 +57,18 @@ void BirthdayPartyAttached::setRsvp(const QDate &d) BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() diff --git a/examples/declarative/extending/signal/birthdayparty.h b/examples/declarative/extending/signal/birthdayparty.h index a2b35cd..ae4dd39 100644 --- a/examples/declarative/extending/signal/birthdayparty.h +++ b/examples/declarative/extending/signal/birthdayparty.h @@ -48,8 +48,8 @@ class BirthdayPartyAttached : public QObject { -Q_OBJECT -Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) + Q_OBJECT + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) public: BirthdayPartyAttached(QObject *object); @@ -62,15 +62,15 @@ private: class BirthdayParty : public QObject { -Q_OBJECT -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) -Q_CLASSINFO("DefaultProperty", "guests") + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; @@ -85,7 +85,7 @@ signals: // ![0] private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/signal/example.qml b/examples/declarative/extending/signal/example.qml index c7d4792..83d6a23 100644 --- a/examples/declarative/extending/signal/example.qml +++ b/examples/declarative/extending/signal/example.qml @@ -5,13 +5,13 @@ BirthdayParty { onPartyStarted: console.log("This party started rockin' at " + time); // ![0] - celebrant: Boy { + host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } Boy { - name: "Joan Hodges" + name: "Leo Hodges" BirthdayParty.rsvp: "2009-07-06" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } diff --git a/examples/declarative/extending/signal/main.cpp b/examples/declarative/extending/signal/main.cpp index 1b23a46..453f688 100644 --- a/examples/declarative/extending/signal/main.cpp +++ b/examples/declarative/extending/signal/main.cpp @@ -60,10 +60,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/signal/person.h b/examples/declarative/extending/signal/person.h index 7a4b9c3..2f444c5 100644 --- a/examples/declarative/extending/signal/person.h +++ b/examples/declarative/extending/signal/person.h @@ -43,14 +43,14 @@ #include #include -#include -class ShoeDescription : public QObject { -Q_OBJECT -Q_PROPERTY(int size READ size WRITE setSize) -Q_PROPERTY(QColor color READ color WRITE setColor) -Q_PROPERTY(QString brand READ brand WRITE setBrand) -Q_PROPERTY(qreal price READ price WRITE setPrice) +class ShoeDescription : public QObject +{ + Q_OBJECT + Q_PROPERTY(int size READ size WRITE setSize) + Q_PROPERTY(QColor color READ color WRITE setColor) + Q_PROPERTY(QString brand READ brand WRITE setBrand) + Q_PROPERTY(qreal price READ price WRITE setPrice) public: ShoeDescription(QObject *parent = 0); @@ -72,10 +72,11 @@ private: qreal m_price; }; -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(ShoeDescription *shoe READ shoe) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(ShoeDescription *shoe READ shoe) public: Person(QObject *parent = 0); @@ -88,14 +89,16 @@ private: ShoeDescription m_shoe; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/valuesource/birthdayparty.cpp b/examples/declarative/extending/valuesource/birthdayparty.cpp index 99d98be..b915f4f 100644 --- a/examples/declarative/extending/valuesource/birthdayparty.cpp +++ b/examples/declarative/extending/valuesource/birthdayparty.cpp @@ -57,18 +57,18 @@ void BirthdayPartyAttached::setRsvp(const QDate &d) BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_celebrant(0) +: QObject(parent), m_host(0) { } -Person *BirthdayParty::celebrant() const +Person *BirthdayParty::host() const { - return m_celebrant; + return m_host; } -void BirthdayParty::setCelebrant(Person *c) +void BirthdayParty::setHost(Person *c) { - m_celebrant = c; + m_host = c; } QDeclarativeListProperty BirthdayParty::guests() @@ -92,12 +92,12 @@ void BirthdayParty::startParty() emit partyStarted(time); } -QString BirthdayParty::speaker() const +QString BirthdayParty::announcement() const { return QString(); } -void BirthdayParty::setSpeaker(const QString &speak) +void BirthdayParty::setAnnouncement(const QString &speak) { qWarning() << qPrintable(speak); } diff --git a/examples/declarative/extending/valuesource/birthdayparty.h b/examples/declarative/extending/valuesource/birthdayparty.h index a9b3102..5f25781 100644 --- a/examples/declarative/extending/valuesource/birthdayparty.h +++ b/examples/declarative/extending/valuesource/birthdayparty.h @@ -49,8 +49,8 @@ class BirthdayPartyAttached : public QObject { -Q_OBJECT -Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) + Q_OBJECT + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) public: BirthdayPartyAttached(QObject *object); @@ -63,26 +63,25 @@ private: class BirthdayParty : public QObject { -Q_OBJECT -Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QDeclarativeListProperty guests READ guests) + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost) + Q_PROPERTY(QDeclarativeListProperty guests READ guests) // ![0] -Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) + Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement) // ![0] -Q_CLASSINFO("DefaultProperty", "guests") + Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); - Person *celebrant() const; - void setCelebrant(Person *); + Person *host() const; + void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; - - QString speaker() const; - void setSpeaker(const QString &); + QString announcement() const; + void setAnnouncement(const QString &); static BirthdayPartyAttached *qmlAttachedProperties(QObject *); @@ -91,7 +90,7 @@ signals: void partyStarted(const QTime &time); private: - Person *m_celebrant; + Person *m_host; QList m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/valuesource/example.qml b/examples/declarative/extending/valuesource/example.qml index ed4d788..5b8c8af 100644 --- a/examples/declarative/extending/valuesource/example.qml +++ b/examples/declarative/extending/valuesource/example.qml @@ -2,19 +2,19 @@ import People 1.0 // ![0] BirthdayParty { - HappyBirthday on speaker { name: "Bob Jones" } + HappyBirthdaySong on announcement { name: "Bob Jones" } // ![0] onPartyStarted: console.log("This party started rockin' at " + time); - celebrant: Boy { + host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } Boy { - name: "Joan Hodges" + name: "Leo Hodges" BirthdayParty.rsvp: "2009-07-06" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } diff --git a/examples/declarative/extending/valuesource/happybirthday.cpp b/examples/declarative/extending/valuesource/happybirthday.cpp deleted file mode 100644 index 0dbbd6e..0000000 --- a/examples/declarative/extending/valuesource/happybirthday.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "happybirthday.h" -#include - -HappyBirthday::HappyBirthday(QObject *parent) -: QObject(parent), m_line(-1) -{ - setName(QString()); - QTimer *timer = new QTimer(this); - QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); - timer->start(1000); -} - -void HappyBirthday::setTarget(const QDeclarativeProperty &p) -{ - m_target = p; -} - -QString HappyBirthday::name() const -{ - return m_name; -} - -void HappyBirthday::setName(const QString &name) -{ - m_name = name; - - m_lyrics.clear(); - m_lyrics << "Happy birthday to you,"; - m_lyrics << "Happy birthday to you,"; - m_lyrics << "Happy birthday dear " + m_name + ","; - m_lyrics << "Happy birthday to you!"; - m_lyrics << ""; -} - -void HappyBirthday::advance() -{ - m_line = (m_line + 1) % m_lyrics.count(); - - m_target.write(m_lyrics.at(m_line)); -} - diff --git a/examples/declarative/extending/valuesource/happybirthday.h b/examples/declarative/extending/valuesource/happybirthday.h deleted file mode 100644 index 8548eb8..0000000 --- a/examples/declarative/extending/valuesource/happybirthday.h +++ /dev/null @@ -1,79 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef HAPPYBIRTHDAY_H -#define HAPPYBIRTHDAY_H - -#include -#include -#include - -#include - -// ![0] -class HappyBirthday : public QObject, public QDeclarativePropertyValueSource -{ -Q_OBJECT -// ![0] -Q_PROPERTY(QString name READ name WRITE setName) -// ![1] -public: - HappyBirthday(QObject *parent = 0); - - virtual void setTarget(const QDeclarativeProperty &); -// ![1] - - QString name() const; - void setName(const QString &); - -private slots: - void advance(); - -private: - int m_line; - QStringList m_lyrics; - QDeclarativeProperty m_target; - QString m_name; -// ![2] -}; -// ![2] - -#endif // HAPPYBIRTHDAY_H - diff --git a/examples/declarative/extending/valuesource/happybirthdaysong.cpp b/examples/declarative/extending/valuesource/happybirthdaysong.cpp new file mode 100644 index 0000000..8ea5c2b --- /dev/null +++ b/examples/declarative/extending/valuesource/happybirthdaysong.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "happybirthdaysong.h" +#include + +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) +: QObject(parent), m_line(-1) +{ + setName(QString()); + QTimer *timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); + timer->start(1000); +} + +void HappyBirthdaySong::setTarget(const QDeclarativeProperty &p) +{ + m_target = p; +} + +QString HappyBirthdaySong::name() const +{ + return m_name; +} + +void HappyBirthdaySong::setName(const QString &name) +{ + m_name = name; + + m_lyrics.clear(); + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday dear " + m_name + ","; + m_lyrics << "Happy birthday to you!"; + m_lyrics << ""; +} + +void HappyBirthdaySong::advance() +{ + m_line = (m_line + 1) % m_lyrics.count(); + + m_target.write(m_lyrics.at(m_line)); +} + diff --git a/examples/declarative/extending/valuesource/happybirthdaysong.h b/examples/declarative/extending/valuesource/happybirthdaysong.h new file mode 100644 index 0000000..3d07909 --- /dev/null +++ b/examples/declarative/extending/valuesource/happybirthdaysong.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef HAPPYBIRTHDAYSONG_H +#define HAPPYBIRTHDAYSONG_H + +#include +#include +#include + +#include + +// ![0] +class HappyBirthdaySong : public QObject, public QDeclarativePropertyValueSource +{ + Q_OBJECT + Q_INTERFACES(QDeclarativePropertyValueSource) +// ![0] + Q_PROPERTY(QString name READ name WRITE setName) +// ![1] +public: + HappyBirthdaySong(QObject *parent = 0); + + virtual void setTarget(const QDeclarativeProperty &); +// ![1] + + QString name() const; + void setName(const QString &); + +private slots: + void advance(); + +private: + int m_line; + QStringList m_lyrics; + QDeclarativeProperty m_target; + QString m_name; +// ![2] +}; +// ![2] + +#endif // HAPPYBIRTHDAYSONG_H + diff --git a/examples/declarative/extending/valuesource/main.cpp b/examples/declarative/extending/valuesource/main.cpp index 2574917..00840ee 100644 --- a/examples/declarative/extending/valuesource/main.cpp +++ b/examples/declarative/extending/valuesource/main.cpp @@ -43,7 +43,7 @@ #include #include #include "birthdayparty.h" -#include "happybirthday.h" +#include "happybirthdaysong.h" #include "person.h" int main(int argc, char ** argv) @@ -52,7 +52,7 @@ int main(int argc, char ** argv) qmlRegisterType(); qmlRegisterType("People", 1,0, "BirthdayParty"); - qmlRegisterType("People", 1,0, "HappyBirthday"); + qmlRegisterType("People", 1,0, "HappyBirthdaySong"); qmlRegisterType(); qmlRegisterType(); qmlRegisterType("People", 1,0, "Boy"); @@ -62,10 +62,10 @@ int main(int argc, char ** argv) QDeclarativeComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); - if (party && party->celebrant()) { - qWarning() << party->celebrant()->name() << "is having a birthday!"; + if (party && party->host()) { + qWarning() << party->host()->name() << "is having a birthday!"; - if (qobject_cast(party->celebrant())) + if (qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; diff --git a/examples/declarative/extending/valuesource/person.h b/examples/declarative/extending/valuesource/person.h index 7a4b9c3..2f444c5 100644 --- a/examples/declarative/extending/valuesource/person.h +++ b/examples/declarative/extending/valuesource/person.h @@ -43,14 +43,14 @@ #include #include -#include -class ShoeDescription : public QObject { -Q_OBJECT -Q_PROPERTY(int size READ size WRITE setSize) -Q_PROPERTY(QColor color READ color WRITE setColor) -Q_PROPERTY(QString brand READ brand WRITE setBrand) -Q_PROPERTY(qreal price READ price WRITE setPrice) +class ShoeDescription : public QObject +{ + Q_OBJECT + Q_PROPERTY(int size READ size WRITE setSize) + Q_PROPERTY(QColor color READ color WRITE setColor) + Q_PROPERTY(QString brand READ brand WRITE setBrand) + Q_PROPERTY(qreal price READ price WRITE setPrice) public: ShoeDescription(QObject *parent = 0); @@ -72,10 +72,11 @@ private: qreal m_price; }; -class Person : public QObject { -Q_OBJECT -Q_PROPERTY(QString name READ name WRITE setName) -Q_PROPERTY(ShoeDescription *shoe READ shoe) +class Person : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(ShoeDescription *shoe READ shoe) public: Person(QObject *parent = 0); @@ -88,14 +89,16 @@ private: ShoeDescription m_shoe; }; -class Boy : public Person { -Q_OBJECT +class Boy : public Person +{ + Q_OBJECT public: Boy(QObject * parent = 0); }; -class Girl : public Person { -Q_OBJECT +class Girl : public Person +{ + Q_OBJECT public: Girl(QObject * parent = 0); }; diff --git a/examples/declarative/extending/valuesource/valuesource.pro b/examples/declarative/extending/valuesource/valuesource.pro index d3409b6..0626c98 100644 --- a/examples/declarative/extending/valuesource/valuesource.pro +++ b/examples/declarative/extending/valuesource/valuesource.pro @@ -8,10 +8,10 @@ QT += declarative SOURCES += main.cpp \ person.cpp \ birthdayparty.cpp \ - happybirthday.cpp + happybirthdaysong.cpp HEADERS += person.h \ birthdayparty.h \ - happybirthday.h + happybirthdaysong.h RESOURCES += valuesource.qrc target.path = $$[QT_INSTALL_EXAMPLES]/declarative/extending/valuesource sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS valuesource.pro -- cgit v0.12