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