Creating a Plugin

The QML engine loads C++ plugins for QML. Such plugins are usually provided in a QML extension module, and can provide types for use by clients in QML documents that import the module. A module requires at least one registered type to be considered valid.

QQmlEngineExtensionPlugin is a plugin interface that lets you 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 QQmlEngineExtensionPlugin and use the Q_PLUGIN_METADATA() macro to register the plugin with the Qt meta object system.

  2. Use the QML_ELEMENT and QML_NAMED_ELEMENT() macros to declare QML types.

  3. Configure your build file.

    CMake:

     
    Sélectionnez
    qt_add_qml_module(<target>
        URI <my.import.name>
        VERSION 1.0
        QML_FILES <app.qml>
        NO_RESOURCE_TARGET_PATH
    )

    qmake:

     
    Sélectionnez
    CONFIG += qmltypes
    QML_IMPORT_NAME = <my.import.name>
    QML_IMPORT_MAJOR_VERSION = <version>
  4. If you're using qmake, create a qmldir file to describe the plugin. Note that CMake will, by default, automatically generate the qmldir file.

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.

The linker might erroneously remove the generated type registration function as an optimization. You can prevent that by declaring a synthetic volatile pointer to the function somewhere in your code. If your module is called "my.module", you would add the forward declaration in global scope:

 
Sélectionnez
void qml_register_types_my_module();

Then add the following snippet of code in the implementation of any function that's part of the same binary as the registration:

 
Sélectionnez
volatile auto registration = &qml_register_types_my_module;
Q_UNUSED(registration);

TimeExample QML Extension Plugin

Suppose there is a new TimeModel C++ class that should be made available as a new QML type. It provides the current time through hour and minute properties. It declares a QML type called Time via QML_NAMED_ELEMENT().

 
Sélectionnez
class TimeModel : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int hour READ hour NOTIFY timeChanged)
    Q_PROPERTY(int minute READ minute NOTIFY timeChanged)
    QML_NAMED_ELEMENT(Time)
    ...

To make this type available, create a plugin class named QExampleQmlPlugin, which is a subclass of QQmlEngineExtensionPlugin. It uses the Q_PLUGIN_METADATA() macro in the class definition to register the plugin with the Qt meta object system using a unique identifier for the plugin.

 
Sélectionnez
class QExampleQmlPlugin : public QQmlEngineExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
};

Build Settings for the Plugin

The build file defines the project as a plugin library, specifies it should be built into the imports/TimeExample directory, and registers the plugin target name.

Using CMake: