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. |
Inherited By: |
Public Functions
QQmlExtensionPlugin(QObject * parent = 0) | |
QUrl | baseUrl() const |
Reimplemented Public Functions
virtual void | initializeEngine(QQmlEngine * engine, const char * uri) |
virtual void | registerTypes(const char * uri) = 0 |
- 31 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
The QQmlExtensionPlugin class provides an abstract base for custom QML extension plugins.
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:
- Subclass QQmlExtensionPlugin, implement registerTypes() method to register types using qmlRegisterType(), and export the class using the Q_PLUGIN_METADATA() macro
- Write an appropriate project file for the plugin
- Create a qmldir file to describe the plugin
QML extension plugins can be used to provide 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.
An 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, like this:
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 class available as a QML type, create a plugin that registers this type with a specific module using qmlRegisterType(). For this example the plugin module will be named TimeExample
(as defined in the project file further below).
class QExampleQmlPlugin : public QQmlExtensionPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") public: void registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("TimeExample")); qmlRegisterType<TimeModel>(uri, 1, 0, "Time"); } };
This registers the TimeModel
class with the 1.0 version of this plugin library, as a QML type called Time
. The Q_ASSERT statement ensures the module is imported correctly by any QML components that use this plugin.
The project file defines the project as a plugin library and specifies it should be built into the imports/TimeExample
directory:
TEMPLATE = lib CONFIG += qt plugin QT += qml DESTDIR = imports/TimeExample TARGET = qmlqtimeexampleplugin ...
Finally, a qmldir file is required in the imports/TimeExample
directory that describes 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:
module TimeExample Clock 1.0 Clock.qml plugin qmlqtimeexampleplugin
Once the project is built and installed, the new Time
element can be used by any QML component that imports the TimeExample
module:
import 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.
Note that the Qt Quick 1 version is called QDeclarativeExtensionPlugin.
See also QQmlEngine::importPlugin() and How to Create Qt Plugins.
Member Function Documentation
QQmlExtensionPlugin::QQmlExtensionPlugin(QObject * parent = 0)
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.
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.
[virtual]
void QQmlExtensionPlugin::initializeEngine(QQmlEngine * engine, const char * uri)
Initializes the extension from the uri using the engine. Here an application plugin might, for example, expose some data or objects to QML, as context properties on the engine's root context.
[pure virtual]
void QQmlExtensionPlugin::registerTypes(const char * uri)
Registers the QML types in the given uri. Subclasses should implement this to call qmlRegisterType() for all types which are provided by the extension plugin.
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.
© 2015 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.