Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qt_add_qml_plugin.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-add-qml-plugin.html
6\ingroup cmake-commands-qtqml
7
8\title qt_add_qml_plugin
9\target qt6_add_qml_plugin
10
11\brief Defines a plugin associated with a QML module.
12
13\section1 Synopsis
14
15\badcode
16qt_add_qml_plugin(
17 target
18 [BACKING_TARGET backing_target]
19 [STATIC | SHARED]
20 [OUTPUT_DIRECTORY]
21 [URI]
22 [CLASS_NAME]
23 [NO_GENERATE_PLUGIN_SOURCE]
24 [NAMESPACE namespace]
25)
26
27\endcode
28
29\versionlessCMakeCommandsNote qt6_add_qml_plugin()
30
31\section1 Description
32
33This command creates the plugin target associated with a QML module. It would
34normally be called internally by \l{qt6_add_qml_module}{qt_add_qml_module()} to
35create or update the plugin associated with its backing target. You should not
36call this function directly unless you have special circumstances that require
37you to create the target in a special way.
38
39The documentation for \l{qt6_add_qml_module}{qt_add_qml_module()} describes
40different structural patterns for how the CMake targets associated with a QML
41module can be arranged. Note that even if the QML module has no separate backing
42target and all functionality is implemented directly in the plugin (not the
43recommended arrangement), you should still call
44\l{qt6_add_qml_module}{qt_add_qml_module()} rather than \c{qt_add_qml_plugin()}.
45
46
47\section1 Arguments
48
49The \c target specifies the name of the target to use for the QML plugin. If it
50does not already exist, it will be created.
51
52\c BACKING_TARGET specifies the name of the backing target that the plugin is
53associated with. The backing target can be the same as the plugin \c target, in
54which case there is only the one merged target, but this is not typically
55recommended (see \l{qt6_add_qml_module}{qt_add_qml_module()} for more
56information). \c BACKING_TARGET should always be provided unless there are
57special circumstances that require the plugin target to be created before the
58backing target. If \c BACKING_TARGET is not provided, a \c URI option must be
59given.
60
61By default, the plugin is created with a type that is compatible with the
62backing target. If the backing target is a static library, the plugin will also
63be created as a static library. If the backing target is a shared library, the
64plugin will be created as a module library. Where no backing target is
65provided or the plugin has no separate backing target, the plugin type can be
66specified with either the \c STATIC or \c SHARED keywords. If the plugin type
67is not determined by any of the preceding conditions, a static plugin will be
68created if Qt was built as static libraries, or a module library plugin
69otherwise.
70
71\c OUTPUT_DIRECTORY specifies the directory where the plugin library will be
72created. It should always be the same location as the QML module's
73\l{Module Definition qmldir Files}{qmldir} file. When \c OUTPUT_DIRECTORY is
74not given, it will be obtained from information stored on the
75\c BACKING_TARGET, where available. Note that this could be different to the
76directory of the backing target's own library. If an output directory cannot be
77obtained from the backing target, the \c CMAKE_CURRENT_BINARY_DIR is used by
78default.
79
80\c URI declares the module identifier of the QML module this plugin is
81associated with. The module identifier is the (dotted URI notation) identifier
82for the QML module. If \c URI is not given, a \c BACKING_TARGET must be
83provided and the backing target must have its URI recorded on it (typically by
84an earlier call to \l{qt6_add_qml_module}{qt_add_qml_module()}).
85
86Each plugin should have a C++ class that registers the module with the QML
87engine. By default, \c{qt_add_qml_plugin()} auto-generates the sources for this
88C++ class, and adds them to the \c{target}'s list of sources. The generated
89plugin class satisfies the requirements of the plugin being optional (see
90\l{Module Definition qmldir Files}). The class name is determined as follows:
91
92\list
93 \li If \c CLASS_NAME has been given, it will be used. It must match the name
94 used in the QML module's \c qmldir file.
95 \li If \c CLASS_NAME has not been given, but \c BACKING_TARGET has, the C++
96 class name will be taken from details recorded on that backing target.
97 Those details are usually recorded by an earlier call to
98 \l{qt_add_qml_module}{qt_add_qml_module()}, and they will match the name
99 used in the generated \c qmldir file. This is the recommended way to
100 provide the class name in most scenarios.
101 \li If the class name still cannot be determined, it is set to the module's
102 URI with dots replaced by underscores, and \c Plugin appended.
103\endlist
104
105If a namespace is given with the \c NAMESPACE keyword, the plugin
106code will be generated into a C++ namespace of this name.
107
108Some plugins may require the plugin class to be written manually. For example,
109the plugin may need to perform additional initialization or register things
110not implemented by the default plugin class. In such cases, the
111\c NO_GENERATE_PLUGIN_SOURCE option can be given. You are then responsible for
112writing your own C++ plugin class and adding it to the \c target. Note that if
113you need to write your own plugin class, it is very unlikely that the plugin
114can be optional. This in turn means that the \c NO_PLUGIN_OPTIONAL keyword
115should be included in the call to \l{qt_add_qml_module}{qt_add_qml_module()}
116when defining the QML module, or else the generated \c qmldir file will be
117incorrect. Make sure your plugin class uses the same class name as determined
118from the logic just above.
119
120*/