aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/qml/qmlplugins.qdoc
blob: e41bba29068a2b75966346ec71c27ed96721cd11 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\page qml-plugins.html
\title QML Plugins
\target qml-plugins
\brief importing Qt C++ functions as plugins


    The \l{The QML Engine}{QML engine} can run Qt C++ applications
    by \l{register-c++-type}{registering} types into the runtime and by loading
    C++ code as plugins. Plugins are imported and labeled as modules and
    its content are available as components.

    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:
    \list 1
    \o Subclass QQmlExtensionPlugin
    \o Implement QQmlExtensionPlugin's
    \l{QQmlExtensionPlugin::}{registerTypes()} method
    \o Register types with qmlRegisterType()
    \o Export the class using the Q_EXPORT_PLUGIN2() macro
    \o Write a project file for the plugin
    \o Create a \l{Writing a qmldir file}{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 Plugin Example

    Suppose there is a new \c TimeModel C++ class that should be made available
    as a new QML element. It provides the current time through \c hour and \c minute
    properties.

    \snippet examples/declarative/cppextensions/plugins/plugin.cpp 0
    \dots

    A plugin class, \c QExampleQMLPlugin, is a subclass of
    \l QQmlExtensionPlugin and it implements the
    \l{QQmlExtensionPlugin::}{registerTypes()} method.

    In the registerTypes() method, the plugin class can
    \l{register-c++-type}{register} the \c TimeModel class to the declarative
    runtime with the qmlRegisterType() function. The Q_EXPORT_PLUGIN2() macro has
    two parameters, the generated plugin name and the class name.

    \snippet examples/declarative/cppextensions/plugins/plugin.cpp plugin
    \codeline
    \snippet examples/declarative/cppextensions/plugins/plugin.cpp export

    The \c TimeModel class receives a \c{1.0} version of this plugin library, as
    a QML type called \c Time. The Q_ASSERT() macro can ensure the module is
    imported correctly by any QML components that use this plugin. The
    \l{Creating QML Types} article has more information about registering C++
    types into the runtime.

    For this example, the TimeExample source directory is in
    \c{com/nokia/TimeExample}. The plugin's module import statement will follow
    this structure.

    The project file, in a \c .pro file, defines the project as a plugin library
    and specifies it should be built into the \c com/nokia/TimeExample
    directory:

    \code
    TEMPLATE = lib
    CONFIG += qt plugin
    QT += declarative

    DESTDIR = com/nokia/TimeExample
    TARGET = qmlqtimeexampleplugin
    ...
    \endcode

    Finally, a \l{Writing a qmldir file}{qmldir file} is required in the \c
    com/nokia/TimeExample directory to specify the plugin. This directory
    includes a \c Clock.qml file that should be bundled with the plugin, so it
    needs to be specified in the \c qmldir file:

    \quotefile examples/declarative/cppextensions/plugins/com/nokia/TimeExample/qmldir

    Once the project is built and installed, the new \c Time component is
    accessible by any QML component that imports the \c com.nokia.TimeExample
    module

    \snippet examples/declarative/cppextensions/plugins/plugins.qml 0

    The full source code is available in the \l {declarative/cppextensions/plugins}{plugins example}.


\section1 Reference

    \list
    \o \l {Tutorial: Writing QML extensions with C++} - contains a chapter
    on creating QML plugins.
    \o \l{Creating QML Types} - information about registering C++ types into
    the runtime.
    \o \l{How to Create Qt Plugins} - information about Qt plugins
    \endlist


*/