aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src/qmllanguageref/modules/qqmlextensionplugin.qdocinc
blob: 771d520f8a22e2a3930d94c4462d68b533f656a7 (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
\l QQmlEngineExtensionPlugin is a plugin interface that lets you
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 Configure your build file.

    CMake:
    \badcode
        qt_add_qml_module(<target>
            URI <my.import.name>
            VERSION 1.0
            QML_FILES <app.qml>
            NO_RESOURCE_TARGET_PATH
        )
    \endcode

    qmake:
    \badcode
        CONFIG += qmltypes
        QML_IMPORT_NAME = <my.import.name>
        QML_IMPORT_MAJOR_VERSION = <version>
    \endcode
\li If you're using qmake, create a
    \l {Module Definition qmldir Files} {qmldir file} to describe the plugin.
    Note that CMake will, by default, automatically generate the
    \l {Module Definition qmldir Files} {qmldir file}.
\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.

\note When using the CMake \l qt_add_qml_module API, a plugin will be generated
automatically for you. It will take care of type registration.
You only need to write a custom plugin if you have special
requirements, such as registering custom image
providers. In that case, pass
\l{NO_GENERATE_PLUGIN_SOURCE} to the \c qt_add_qml_module
call to disable the generation of the default plugin.

The linker might erroneously remove the generated type registration
function as an optimization. You can prevent that by declaring a synthetic
volatile pointer to the function somewhere in your code. If your module is
called "my.module", you would add the forward declaration in global scope:

\code
void qml_register_types_my_module();
\endcode

Then add the following snippet of code in the implementation of any function
that's part of the same binary as the registration:

\code
volatile auto registration = &qml_register_types_my_module;
Q_UNUSED(registration);
\endcode