QQmlExtensionPlugin Class

The QQmlExtensionPlugin class provides an abstract base for custom QML extension plugins. More...

Header: #include <QQmlExtensionPlugin>
qmake: QT += qml
Since: Qt 5.0
Inherits: QObject

Public Functions

QQmlExtensionPlugin(QObject *parent = Q_NULLPTR)
QUrl baseUrl() const

Reimplemented Public Functions

virtual void registerTypes(const char *uri) override = 0
  • 32 public functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 2 signals inherited from QObject
  • 11 static public members inherited from QObject
  • 9 protected functions inherited from QObject

Detailed Description



To write a QML extension plugin:

  1. Subclass QQmlExtensionPlugin
  2. Write a project file for the plugin
  3. Create a qmldir file to describe the plugin


Plugin Example


class TimeModel : public QObject
    Q_PROPERTY(int hour READ hour NOTIFY timeChanged)
    Q_PROPERTY(int minute READ minute NOTIFY timeChanged)

To make this type available, we create a plugin class named QExampleQmlPlugin which is a subclass of QQmlExtensionPlugin. It overrides the registerTypes() method in order to register the TimeModel type using qmlRegisterType(). It also 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.

class QExampleQmlPlugin : public QQmlExtensionPlugin
    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)

    void registerTypes(const char *uri)
        Q_ASSERT(uri == QLatin1String("TimeExample"));
        qmlRegisterType<TimeModel>(uri, 1, 0, "Time");

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


CONFIG += qt plugin
QT += qml

DESTDIR = imports/TimeExample
TARGET = qmlqtimeexampleplugin
SOURCES += qexampleqmlplugin.cpp


module TimeExample
Clock 1.0 Clock.qml
plugin qmlqtimeexampleplugin




import TimeExample 1.0 // import types from the plugin


        id: time

    hours: time.hour
    minutes: time.minute


The full source code is available in the plugins example.


Note that the Qt Quick 1 version is called QDeclarativeExtensionPlugin.


Member Function Documentation

QQmlExtensionPlugin::QQmlExtensionPlugin(QObject *parent = Q_NULLPTR)

Constructs a QML extension plugin with the given parent.


QUrl QQmlExtensionPlugin::baseUrl() const

Returns the URL of the directory from which the extension is loaded.

This is useful when the plugin also needs to load QML files or other assets from the same directory.

This function was introduced in Qt 5.1.

[override virtual] void QQmlExtensionPlugin::initializeEngine(QQmlEngine *engine, const char *uri)


[override pure virtual] void QQmlExtensionPlugin::registerTypes(const char *uri)


The uri is an identifier for the plugin generated by the QML engine based on the name and path of the extension's plugin library.
