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  · 

QML Plugins

The QML engine can run Qt C++ applications by registering types into the runtime and by loading C++ code as plugins. Plugins are imported and labeled as modules and its content are available as components.

QQmlExtensionPlugin is a plugin interface that makes it possible to create QML extensions that can be loaded dynamically into QML applications. These extensions allow custom QML types to be made available to the QML engine.

To write a QML extension plugin:

  1. Subclass QQmlExtensionPlugin
  2. Implement QQmlExtensionPlugin's registerTypes() method
  3. Register types with qmlRegisterType()
  4. Export the class using the Q_EXPORT_PLUGIN2() macro
  5. Write a project file for the plugin
  6. Create a qmldir file to describe the plugin

QML extension plugins are for either application-specific or library-like plugins. Library plugins should limit themselves to registering types, as any manipulation of the engine's root context may cause conflicts or other issues in the library user's code.

Plugin Example

Suppose there is a new TimeModel C++ class that should be made available as a new QML element. It provides the current time through hour and minute properties.

     ...

A plugin class, QExampleQMLPlugin, is a subclass of QQmlExtensionPlugin and it implements the registerTypes() method.

In the registerTypes() method, the plugin class can register the TimeModel class to the declarative runtime with the qmlRegisterType() function. The Q_EXPORT_PLUGIN2() macro has two parameters, the generated plugin name and the class name.


The TimeModel class receives a 1.0 version of this plugin library, as a QML type called Time. The Q_ASSERT() macro can ensure the module is imported correctly by any QML components that use this plugin. The C++ Types as QML Types article has more information about registering C++ types into the runtime.

For this example, the TimeExample source directory is in com/nokia/TimeExample. The plugin's module import statement will follow this structure.

The project file, in a .pro file, defines the project as a plugin library and specifies it should be built into the com/nokia/TimeExample directory:

 TEMPLATE = lib
 CONFIG += qt plugin
 QT += declarative

 DESTDIR = com/nokia/TimeExample
 TARGET = qmlqtimeexampleplugin
 ...

Finally, a qmldir file is required in the com/nokia/TimeExample directory to specify the plugin. This directory includes a Clock.qml file that should be bundled with the plugin, so it needs to be specified in the qmldir file:


Once the project is built and installed, the new Time component is accessible by any QML component that imports the com.nokia.TimeExample module


The full source code is available in the plugins example.

Reference

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