QQmlExtensionPlugin¶
The
QQmlExtensionPlugin
class provides an abstract base for custom QML extension plugins. More…
Detailed Description¶
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:
Write a project file for the plugin
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.
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 throughhour
andminute
properties.class TimeModel : public QObject { Q_OBJECT 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 ofQQmlExtensionPlugin
. It overrides theregisterTypes()
method in order to register theTimeModel
type usingqmlRegisterType()
. It also uses theQ_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_OBJECT Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid) public: void registerTypes(const char *uri) override { Q_ASSERT(uri == QLatin1String("TimeExample")); qmlRegisterType<TimeModel>(uri, 1, 0, "Time"); } };This registers the
TimeModel
class with version1.0
of this plugin library, as a QML type calledTime
. TheQ_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.
Project settings for the plugin¶
Additionally, the project file (
.pro
) defines the project as a plugin library, specifies it should be built into theimports/TimeExample
directory, and registers the plugin target name and various other details:TEMPLATE = lib CONFIG += qt plugin QT += qml DESTDIR = imports/TimeExample TARGET = qmlqtimeexampleplugin SOURCES += qexampleqmlplugin.cpp
Plugin definition in the qmldir¶
Finally, a qmldir file is required in the
imports/TimeExample
directory to describe the plugin and the types that it exports. The plugin includes aClock.qml
file along with theqmlqtimeexampleplugin
that is built by the project (as shown above in the.pro
file) so both of these need to be specified in theqmldir
file:<Code snippet "/tmp/snapshot-qt5full-5.12/qt5/qtbase/qmlextensionplugins/imports/TimeExample/qmldir" not found>To make things easier for this example, the TimeExample source directory is in
imports/TimeExample
, and we build in-source. However, the structure of the source directory is not so important, as theqmldir
file can specify paths to installed QML files.What is important is the name of the directory that the qmldir is installed into. When the user imports our module, the QML engine uses the module identifier (
TimeExample
) to find the plugin, and so the directory in which it is installed must match the module identifier.Once the project is built and installed, the new
Time
component is accessible by any QML component that imports theTimeExample
moduleimport TimeExample 1.0 // import types from the plugin Clock { // this class is defined in QML (imports/TimeExample/Clock.qml) Time { // this class is defined in C++ (plugin.cpp) id: time } hours: time.hour minutes: time.minute }The full source code is available in the plugins example .
The Writing QML Extensions with C++ tutorial also contains a chapter on creating QML plugins.
See also
importPlugin()
How to Create Qt Plugins
-
class
QQmlExtensionPlugin
([parent=None])¶ - param parent
QObject
Constructs a QML extension plugin with the given
parent
.Note that this constructor is invoked automatically by the
Q_PLUGIN_METADATA()
macro, so there is no need for calling it explicitly.
-
PySide2.QtQml.QQmlExtensionPlugin.
baseUrl
()¶ - Return type
QUrl
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.
© 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.