Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Creating QML Types

The QML engine can instantiate any Qt C++ construct such as properties, functions, and data models into the QML context allowing the constructs to be accessible from within QML.

Register a Type

In an application or a plugin, the qmlRegisterType template will register a class to the QML engine.

 template<typename T>
 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)

qmlRegisterType() registers the C++ type T with the QML system, and makes it available to the QML context under the name qmlName in library uri version versionMajor.versionMinor. The qmlName can be the same as the C++ type name.

Suppose that a Person class defined in a C++ is to be exposed into the QML context. The class must be a subclass of QObject and have a default constructor. The properties created with the Q_PROPERTY macro are visible in the QML context as well.


The application registers the class to the runtime with the qmlRegisterType().


The Person type is then imported with the "People 1.0" module and its properties are accessible in a QML file.


The Adding Types example demonstrates as usage of the qmlRegisterType().

Alternatively, these functions provide a way for other types of C++ types to be visible in the QML context.

Qt Objects and Interfaces

QML can bind to complex objects such as pointers to objects or lists. As QML is typesafe, the QML engine ensures that only valid types are assigned to these properties.

The QML engine treats pointers to objects or Qt interfaces the same way as regular properties. Thus, the lists or pointers are created as properties using the Q_PROPERTY() macro.


The host is an exposed property that can bind to objects or lists of objects. The property type, in this case Person, must be registered into the runtime.

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:

     template<typename T>
     int qmlRegisterInterface(const char *typeName)

This function registers the C++ interface T with the QML system as typeName.

Following registration, QML can coerce objects that implement this interface for assignment to appropriately typed properties.

Exposing Qt C++ Properties

The QML engine utilizes Qt's Property System and in effect, QML property bindings also use Qt properties.

Essentially, a Qt C++ property has a write function, read function, and has a signal function. QML properties are inheritely public, both readable and writable, albeit type-safe. QML properties may also have signals which are emitted when the property value or binding changes.

The QML property equivalent of a Qt C++ property is created as a property with the Q_PROPERTY() macro. There needs to be C++ functions assigned as the property's read, write, and signal handler function.

The Register a Type section mentions that the Person class has properties that are exposed to the QML context. The QML properties are created with the Q_PROPERTY macro. The macro associates the properties to the read, write, and singal functions in its argument.

 Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged)

A Shoe class might have an integer property called size. We set the size() function as the READ function and the setSize() function to be the WRITE function. In a QML application, when a property is read, the size() is called and when the property's binding changes, the setSize() is called. The READ function, by definition, must return the same type as the property.

We may also connect a signal to a property. The size property may have a shoeChanged signal indicated after the NOTIFY parameter of the macro. The shoeChanged becomes a QML signal and the runtime will create QML handler called onShoeChanged. Whenever the size property's binding changes, the shoeChanged signal is emitted and the onShoeChanged handler is invoked. In the handler, commands such as JavaScript expressions can perform clean-up operations or call other functions.

Note: The QML signal handler will always be named on<Property-name>Changed, regardless of the name used for the NOTIFY signal in C++. We recommend using <property-name>Changed() for the NOTIFY signal in C++.

We may also make the property a read-only property by placing CONSTANT in the parameter. Changing the binding will generate an error.

 //A read-only property
 Q_PROPERTY(int size READ size CONSTANT)

Default Property

When imported, QML components will bind their children to their designated default property. This is helpful, for example, to redirect any declared child components to a property of another component.

The runtime can set a property to be the default property by tagging the property with DefaultProperty in The Q_CLASSINFO() macro.

     Q_CLASSINFO("DefaultProperty", "pipe")

The property tagged as default property, pipe, can only be an object property, or a list property.

A default property is optional. A derived class inherits its base class's default property, but may override it in its own declaration. The pipe property can refer to a property declared in the class itself, or a property inherited from a base class.

The Default Property example uses default properties to assign the children of a component to a specific property.

Grouped Properties

A property group may be functionally defined as a set of related properties. For example, the anchors are a group of related properties. In practice, property groups resemble a parent object where the individual properties are accessed as children.

A grouped property's member properties are accessed using the <group>.<property> notation. For example, shoe.color is the way to access the color property in the shoe property group .


Alternatively, the group can be accessed as a set.


A grouped property block is implemented as a read-only object property. The shoe property shown is declared like this:


The ShoeDescription type declares the properties available to the grouped property block - in this case the size, color, brand and price properties.

Grouped property blocks may declared and accessed be recusively.

Extending QML - Grouped Properties Example shows the complete code used to implement the shoe property grouping.

Attached Properties

Attached properties annotate or add properties to another type or component. For example, the Keys attaching type contains attached properties that other elements may use to respond to key input. Conceptually, attached properties are a type exporting a set of additional properties that can be set on any other object instance.

The attaching type is a QObject derived type. The properties on the attaching type are those that become available for use as attached properties.


The BirthdayParty is called the attaching type and the Boy instance the attachee object instance. The property rsvp is the attached property.

Any Qt C++ type can become an attaching type by declaring the qmlAttachedProperties() a public member function and declaring that the class has QML_HAS_ATTACHED_PROPERTIES.

     static AttachedPropertiesType *qmlAttachedProperties(QObject *object);

This static pointer returns an attachment object, of type AttachedPropertiesType, for the attachee object instance. It is customary, though not strictly required, for the attachment object to be parented to object to prevent memory leaks. The Birthday class has BirthdayPartyAttached attached properties.


The QML_DECLARE_TYPEINFO() macro can notify the runtime that the type has attached properties with the QML_HAS_ATTACHED_PROPERTIES argument.


The qmlAttachedProperties method will be 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 object is destroyed.

A common usage scenario is for a type to enhance the properties available to its children in order to gather instance specific data.


However, as a QML 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. Instead, BirthdayParty could be a separate component with a property rsvp.

     GraduationParty {
         Boy { BirthdayParty.rsvp: "2009-06-01" }
     }

From C++, including the attaching type implementation, the attachment object for an instance can be accessed using the following method:

     template<typename T>
     QObject *qmlAttachedPropertiesObject<T>(QObject *attachee, bool create = true);

This returns the attachment object attached to attachee by the attaching type T. If type T is not a valid attaching type, this method always returns 0. If create is true, a valid attachment object will always be returned, creating it if it does not already exist. If create is false, the attachment object will only be returned if it has previously been created.

The rsvp properties of each guest in the Birthday party is accessible through the qmlAttachedPropertiesObject function.


The Attached Properties Example demonstrates the creation of attached properties with a birthday party scenario.

Object and List Properties

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. As QML is typesafe it ensures that only valid types are assigned to these properties, just like it does for primitive types.

Properties that are pointers to objects or Qt interfaces are declared with the Q_PROPERTY() macro, just like other properties. The host property declaration looks like this:


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:

     template<typename T>
     int qmlRegisterInterface(const char *typeName)

qmlRegisterInterface registers the C++ interface T with the QML system as typeName.

Following registration, QML can coerce objects that implement this interface for assignment to appropriately typed properties.


The guests property is a list property of Person objects. A list of Person objects are bound to the BirthdayParty's host property, and assigns three Person objects to the guests property.

Properties that are lists of objects or Qt interfaces are also declared with the Q_PROPERTY() macro. However, list properties must have the type QQmlListProperty<T>.


As with the other property types, the type of list content, T, must be registered into the runtime.


Extending QML - Object and List Property Types Example shows the complete code used to create the BirthdayParty type. For more information, visit QQmlListProperty<T> for creating list properties.

Sequence Types

Certain C++ sequence types are supported transparently in QML as JavaScript Array types. In particular, QML currently supports:

  • QList<int>
  • QList<qreal>
  • QList<bool>
  • QList<QString> and QStringList
  • QList<QUrl>

These sequence types are implemented directly in terms of the underlying C++ sequence. There are two ways in which such sequences can be exposed to QML: as a Q_PROPERTY of the given sequence type; or as the return type of a Q_INVOKABLE method. There are some differences in the way these are implemented, which are important to note.

If the sequence is exposed as a Q_PROPERTY, accessing any value in the sequence by index will cause the sequence data to be read from the QObject's property, then a read to occur. Similarly, modifying any value in the sequence will cause the sequence data to be read, and then the modification will be performed and the modified sequence will be written back to the QObject's property.

If the sequence is returned from a Q_INVOKABLE function, access and mutation is much cheaper, as no QObject property read or write occurs; instead, the C++ sequence data is accessed and modified directly.

Other sequence types are not supported transparently, and instead an instance of any other sequence type will be passed between QML and C++ as an opaque QVariantList.

Important Note: There are some minor differences between the semantics of such sequence Array types and default JavaScript Array types which result from the use of a C++ storage type in the implementation. In particular, deleting an element from an Array will result in a default-constructed value replacing that element, rather than an Undefined value. Similarly, setting the length property of the Array to a value larger than its current value will result in the Array being padded out to the specified length with default-constructed elements rather than Undefined elements. Finally, the Qt container classes support signed (rather than unsigned) integer indexes; thus, attempting to access any index greater than INT_MAX will fail.

The default-constructed values for each sequence type are as follows:

QList<int>integer value 0
QList<qreal>real value 0.0
QList<bool>boolean value false
QList<QString> and QStringListempty QString
QList<QUrl>empty QUrl

If you wish to remove elements from a sequence rather than simply replace them with default constructed values, do not use the indexed delete operator ("delete sequence[i]") but instead use the splice function ("sequence.splice(startIndex, deleteCount)").

Property Signals

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 NOTIFY signal for this determination.

Here is the host property declaration:


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.

To prevent loops or excessive evaluation, developers should ensure that the signal is only emitted whenever the property's value is actually changed. If 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.

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 scenario.

Consider a custom type, TestElement, that has two properties, a and b. Property a does not have a NOTIFY signal, and property b does have a NOTIFY signal.

     TestElement {
         // This is OK
         a: b
     }
     TestElement {
         // Will NOT work
         b: a
     }

The presence of a NOTIFY signal does incur a small overhead. There are cases where a property's value is set at object construction time, and does not subsequently change. The most common case of this is when a type uses Grouped Properties, and the grouped property object is allocated once, and only freed when the object is deleted. In these cases, the CONSTANT attribute may be added to the property declaration instead of a NOTIFY signal.


Extreme care must be taken here or applications using your type may misbehave. The CONSTANT attribute should only be used for properties whose value is set, and finalized, only in the class constructor. All other properties that want to be used in bindings should have a NOTIFY signal instead.

Extending QML - Binding Example shows the BirthdayParty example updated to include NOTIFY signals for use in binding.

Signals Support

A signal in Qt C++ is readily available as a QML signal. A signal will have a corresponding signal handler, created automatically. The handler name will have on prepended at the beginning of the name. The first character of the signal is uppercased for the signal handler. The signal parameter is also availabe to the QML signal.


The QML engine will create a handler for the partyStarted signal called onPartyStarted.


Classes may have multiple signals with the same name, but only the final signal is accessible as a QML signal. Note that signals with the same name but different parameters cannot be distinguished from one another.

Signal parameters are exposed and can be any one of the QML basic types as well registered object types. Accessing unregistered types will not generate an error, but the parameter value will not be accessible from the handler.

To use signals from items not created in QML, access their signals with the Connections element.

Additionally, if a property is added to a C++ class, all QML elements based on that C++ class will have a value-changed signal handler for that property. The name of the signal handler is on<Property-name>Changed, with the first letter of the property name being upper case.

The Signal Support Example shows an example application exposing signals to a QML component.

Exposing Methods

The Q_INVOKABLE macro exposes any Qt C++ method as a QML method.


In a QML file, we can invoke the method as we would a JavaScript expression.


Methods example uses the Q_INVOKABLE method to expose methods and demonstrates some usages of the method in an application.

An alternative to the Q_INVOKABLE macro is to declare the C++ method as a slot.

     slots:
         void invite(const QString &name);

Type Revisions and Versions

Type revisions and versions allow new properties or methods to exist in the new version while remaining compatible with previous versions.

Consider these two QML files:

     // main.qml
     import QtQuick 1.0
     Item {
         id: root
         MyComponent {}
     }
     // MyComponent.qml
     import MyModule 1.0
     CppItem {
         value: root.x
     }

where CppItem maps to the C++ class QCppItem.

If the author of QCppItem adds a root property to QCppItem in a new version of the module, root.x now resolves to a different value because root is also the id of the top level component. The author could specify that the new root property is available from a specific minor version. This permits new properties and features to be added to existing elements without breaking existing programs.

The REVISION tag is used to mark the root property as added in revision 1 of the class. Methods such as Q_INVOKABLE's, signals and slots can also be tagged for a revision using the Q_REVISION(x) macro:

     class CppElement : public BaseObject
     {
         Q_OBJECT
         Q_PROPERTY(int root READ root WRITE setRoot NOTIFY rootChanged REVISION 1)

     signals:
         Q_REVISION(1) void rootChanged();
     };

To register the new class revision to a particular version the following function is used:

     template<typename T, int metaObjectRevision>
     int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)

To register CppElement version 1 for MyModule 1.1:

     qmlRegisterType<QCppElement,1>("MyModule", 1, 1, "CppElement")

root is only available when MyModule 1.1 is imported.

For the same reason, new elements introduced in later versions should use the minor version argument of qmlRegisterType.

This feature of the language allows for behavioural changes to be made without breaking existing applications. Consequently QML module authors should always remember to document what changed between minor versions, and QML module users should check that their application still runs correctly before deploying an updated import statement.

You may also register the revision of a base class that your module depends upon using the qmlRegisterRevision() function:

     template<typename T, int metaObjectRevision>
     int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)

For example, if BaseObject is changed and now has a revision 1, you can specify that your module uses the new revision:

     qmlRegisterRevision<BaseObject,1>("MyModule", 1, 1);

This is useful when deriving from base classes not declared as part of your module, e.g. when extending classes from the QtQuick library.

The revision feature of QML allows for behavioral changes without breaking existing applications. Consequently, QML module authors should always remember to document what changed between minor versions, and QML module users should check that their application still runs correctly before deploying an updated import statement.

Inheritance and Coercion

QML supports C++ inheritance hierarchies 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.


The QML snippet shown above assigns a Boy object to the BirthdayParty's host property, and assigns three other objects to the guests property. Both the host and the guests properties binds to the Person type, but the assignment is valid as both the Boy and Girl objects inherit from Person.

To assign to a property, the property's type must have been registered to the declarative runtime. If a type that acts purely as a base class that cannot be instantiated from QML needs to be registered as well. The qmlRegisterType() is useful for this occasion.

         template<typename T>
         int qmlRegisterType()

This function registers the C++ type 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.


The Person class is registered withouth the parameters. Both the Boy and Girl class derive from the Person class.

Type T must inherit QObject, but there are no restrictions on whether it is concrete or the signature of its constructor.

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 error occur.

The Inheritance and Coercion Example shows the complete code used to create the Boy and Girl types.

Extension Objects


The leftMargin property is a new property to an existing C++ type, QLineEdit, without modifying its source code.

When integrating existing classes and technology into QML, APIs will often need tweaking to fit better into the declarative environment. Although the best results are usually obtained by modifying the original classes directly, if this is either not possible or is complicated by some other concerns, extension objects allow limited extension possibilities without direct modifications.

Extension objects add additional properties to an existing type. Extension objects can only add properties, not signals or methods. An extended type definition allows the programmer to supply an additional type, known as the extension type, when registering the class. The properties are transparently merged with the original target class when used from within QML.

The qmlRegisterExtendedType() is for registering extended types. Note that it has two forms.

     template<typename T, typename ExtendedT>
     int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)

     template<typename T, typename ExtendedT>
     int qmlRegisterExtendedType()

functions should be used instead of the regular qmlRegisterType() variations. The arguments are identical to the corresponding non-extension registration functions, except for the ExtendedT parameter which is the type of the extension object.

An extension class is a regular QObject, with a constructor that takes a QObject pointer. However, the extension class creation is delayed until the first extended property is accessed. The extension class is created and the target object is passed in as the parent. When the property on the original is accessed, the corresponding property on the extension object is used instead.

The Extension Objects example demonstrates a usage of extension objects.

Property Value Sources


The QML snippet shown above applies a property value source to the announcement property. A property value source generates a value for a property that changes over time.

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 example shown here is rather contrived: the announcement property of the BirthdayParty object is a string that is printed every time it is assigned and the HappyBirthdaySong value source generates the lyrics of the song "Happy Birthday".


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.

Property value sources are special types that derive from the QQmlPropertyValueSource base class. This base class contains a single method, QQmlPropertyValueSource::setTarget(), that the QML engine invokes when associating the property value source with a property. The relevant part of the HappyBirthdaySong type declaration looks like this:


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 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 the type to also be used in contexts other than just as a value source.

Extending QML - Property Value Source Example shows the complete code used to implement the HappyBirthdaySong property value source.

Optimization and Other Considerations

The QML Engine article suggests possible optimization considerations as memory management and QVariant type usages.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web