QML PluginsThe 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:
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 ExampleSuppose 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
|