aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src/qmllanguageref/modules/qqmlextensionplugin.qdocinc
blob: 496245820a503b32dbdf68720d920e42343798bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
\l QQmlEngineExtensionPlugin 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:
\list 1
\li Subclass \l QQmlEngineExtensionPlugin and use the Q_PLUGIN_METADATA() macro
    to register the plugin with the Qt meta object system.
\li Use the \l QML_ELEMENT and \l QML_NAMED_ELEMENT() macros to declare
    QML types.
\li Write a project file for the plugin. Add:
    \list
        \li \c {CONFIG += qmltypes} to instruct the build system to generate
            QML types.
        \li \c {QML_IMPORT_NAME = <my.import.name>} to specify the import name.
        \li \c {QML_IMPORT_MAJOR_VERSION = <version>} to specify the import
            major version.
    \endlist
\li Create a \l{Module Definition qmldir Files}{qmldir file} to
    describe the plugin
\endlist

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.

\section1 TimeExample QML extension plugin

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

\snippet qmlextensionplugins/timemodel.h 0
\dots

To make this type available, we create a plugin class named \c QExampleQmlPlugin
which is a subclass of \l 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.

\snippet qmlextensionplugins/plugin.cpp plugin

\section1 Project settings for the plugin

Additionally, the project file (\c .pro) defines the project as a plugin library,
specifies it should be built into the \c imports/TimeExample directory, and registers
the plugin target name and various other details:

\code
TEMPLATE = lib
CONFIG += qt plugin qmltypes
QT += qml

QML_IMPORT_NAME = TimeExample
QML_IMPORT_MAJOR_VERSION = 1

DESTDIR = imports/$$QML_IMPORT_NAME
TARGET  = qmlqtimeexampleplugin

SOURCES += qexampleqmlplugin.cpp
\endcode

This registers the \c TimeModel class with the import \c{TimeExample 1.0}, as
a QML type called \c Time. The \l{Defining QML Types from C++} article has more
information about registering C++ types for usage in QML.

\section1 Plugin definition in the qmldir

Finally, a \l{Module Definition qmldir Files}{qmldir file} is required
in the \c imports/TimeExample directory to describe the plugin and the types that it
exports. The plugin includes a \c Clock.qml file along with the \c qmlqtimeexampleplugin
that is built by the project (as shown above in the \c .pro file) so both of these
need to be specified in the \c qmldir file:

\quotefile qmlextensionplugins/imports/TimeExample/qmldir

To make things easier for this example, the TimeExample source directory is in
\c{imports/TimeExample}, and we build
\l{Source, Build, and Install Directories}{in-source}. However, the structure
of the source directory is not so important, as the \c qmldir 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
\l{Contents of a Module Definition qmldir File}{module identifier}
(\c 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 \c Time component is
accessible by any QML component that imports the \c TimeExample
module

\snippet qmlextensionplugins/plugins.qml 0

The full source code is available in the \l {qmlextensionplugins}{plugins example}.