summaryrefslogtreecommitdiffstats
path: root/doc/src/howtos/plugins-howto.qdoc
blob: 25d0dd63d7f76f76d758bab8cb798279444009bb (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \page plugins-howto.html
    \title How to Create Qt Plugins
    \brief A guide to creating plugins to extend Qt's applications and functionalities.

    \ingroup frameworks-technologies
    \ingroup qt-basic-concepts
    \ingroup best-practices


    \keyword QT_DEBUG_PLUGINS
    \keyword QT_NO_PLUGIN_CHECK

    Qt provides two APIs for creating plugins:

    \list
    \li A high-level API for writing extensions to Qt itself, such as custom
    database drivers, image formats, text codecs, and custom styles.
    \li A low-level API for extending Qt applications.
    \endlist

    For example, if you want to write a custom QStyle subclass and
    have Qt applications load it dynamically, you would use the
    higher-level API.

    Since the higher-level API is built on top of the lower-level API,
    some issues are common to both.

    If you want to provide plugins for use with \QD, see
    \l {Creating Custom Widget Plugins}.

    \section1 The High-Level API: Writing Qt Extensions

    Writing a plugin that extends Qt itself is achieved by
    subclassing the appropriate plugin base class, implementing a few
    functions, and adding a macro.

    There are several plugin base classes. Derived plugins are stored
    by default in sub-directories of the standard plugin directory. Qt
    will not find plugins if they are not stored in the appropriate directory.

    The following table summarizes the plugin base classes. Some of the classes
    are private, and are therefore not documented. You can use them, but there
    is no compatibility promise with later Qt versions.

    \table
    \header
        \li Base Class
        \li Directory Name
        \li Qt Module
        \li Key Case Sensitivity
    \row
        \li QAccessibleBridgePlugin
        \li \c accessiblebridge
        \li \l{Qt GUI}
        \li Case Sensitive

    \row
        \li QImageIOPlugin
        \li \c imageformats
        \li \l{Qt GUI}
        \li Case Sensitive
    \row
        \li QPictureFormatPlugin (obsolete)
        \li \c pictureformats
        \li \l{Qt GUI}
        \li Case Sensitive
    \row
        \li QBearerEnginePlugin
        \li \c bearer
        \li \l{Qt Network}
        \li Case Sensitive
    \row
        \li QPlatformInputContextPlugin
        \li \c platforminputcontexts
        \li \l{Qt Platform Abstraction}
        \li Case Insensitive
    \row
        \li QPlatformIntegrationPlugin
        \li \c platforms
        \li \l{Qt Platform Abstraction}
        \li Case Insensitive
    \row
       \li QPlatformThemePlugin
        \li \c platformthemes
        \li \l{Qt Platform Abstraction}
        \li Case Insensitive
    \omit //! Exclude modules not in 6.0
    \row
        \li QGeoPositionInfoSourceFactory
        \li \c position
        \li \l{Qt Positioning}
        \li Case Sensitive
    \endomit //! Exclude modules not in 6.0
    \row
        \li QPlatformPrinterSupportPlugin
        \li \c printsupport
        \li \l{Qt Print Support}
        \li Case Insensitive
    \row
        \li QSGContextPlugin
        \li \c scenegraph
        \li \l{Qt Quick}
        \li Case Sensitive
    \omit //! Exclude modules not in 6.0
    \row
        \li QSensorGesturePluginInterface
        \li \c sensorgestures
        \li \l{Qt Sensors}
        \li Case Sensitive
    \row
        \li QSensorPluginInterface
        \li \c sensors
        \li \l{Qt Sensors}
        \li Case Sensitive
    \endomit //! Exclude modules not in 6.0
    \row
        \li QSqlDriverPlugin
        \li \c sqldrivers
        \li \l{Qt SQL}
        \li Case Sensitive
    \row
        \li QIconEnginePlugin
        \li \c iconengines
        \li \l{Qt SVG}
        \li Case Insensitive
    \row
        \li QAccessiblePlugin
        \li \c accessible
        \li \l{Qt Widgets}
        \li Case Sensitive
    \row
        \li QStylePlugin
        \li \c styles
        \li \l{Qt Widgets}
        \li Case Insensitive
    \endtable

    If you have a new document viewer class called \c JsonViewer that you
    want to make available as a plugin, the class needs to be defined
    as follows (\c jsonviewer.h):

    \snippet demos/documentviewer/plugins/jsonviewer/jsonviewer.h pluginHeader
    \snippet demos/documentviewer/plugins/jsonviewer/jsonviewer.h pluginPrivateMembers

    Ensure that the class implementation is located in a \c .cpp file:

    \snippet demos/documentviewer/plugins/jsonviewer/jsonviewer.cpp pluginCpp

    In addition, a json file (\c jsonviewer.json) containing meta data
    describing the plugin is required for most plugins. For document viewer
    plugins it simply contains the name of the viewer plugin.

    \code
    { "Keys": [ "jsonviewer" ] }
    \endcode

    The type of information that needs to be provided in the json file
    is plugin dependent. See the class documentation for
    details on the information that needs to be contained in the
    file.

    For database drivers, image formats, text codecs, and most other
    plugin types, no explicit object creation is required. Qt will
    find and create them as required.

    Plugin classes may require additional functions to be
    implemented. See the class documentation for details of the
    virtual functions that must be reimplemented for each type of
    plugin.

    The \l{Document Viewer} {Document Viewer Demo} shows how to implement a plugin
    that displayes structured contents of a file. Each plugin therefore
    reimplements virtual functions, which
    \list
    \li identify the plugin
    \li return the MIME types it supports
    \li inform whether there is content to display and
    \li how contents are presented
    \endlist

    \snippet demos/documentviewer/plugins/jsonviewer/jsonviewer.h pluginReimp

    \section1 The Low-Level API: Extending Qt Applications

    In addition to Qt itself, Qt applications can be extended
    through plugins. This requires the application to detect and load
    plugins using QPluginLoader. In that context, plugins may provide
    arbitrary functionality and are not limited to database drivers,
    image formats, text codecs, styles, and other types of plugins
    that extend Qt's functionality.

    Making an application extensible through plugins involves the
    following steps:

    \list 1
    \li Define a set of interfaces (classes with only pure virtual
       functions) used to talk to the plugins.
    \li Use the Q_DECLARE_INTERFACE() macro to tell Qt's
       \l{meta-object system} about the interface.
    \li Use QPluginLoader in the application to load the plugins.
    \li Use qobject_cast() to test whether a plugin implements a given
       interface.
    \endlist

    Writing a plugin involves these steps:

    \list 1
    \li Declare a plugin class that inherits from QObject and from the
       interfaces that the plugin wants to provide.
    \li Use the Q_INTERFACES() macro to tell Qt's \l{meta-object
       system} about the interfaces.
    \li Export the plugin using the Q_PLUGIN_METADATA() macro.
    \endlist

    For example, here's the definition of an interface class:

    \snippet demos/documentviewer/app/viewerinterfaces.h header

    Here's the interface declaration:

    \snippet demos/documentviewer/app/viewerinterfaces.h macros

    See also \l{Creating Custom Widgets for Qt Widgets Designer} for information
    about issues that are specific to \QD.

    \section1 Locating Plugins

    Qt applications automatically know which plugins are available,
    because plugins are stored in the standard plugin subdirectories.
    Because of this, applications don't require any code to find and load
    plugins, since Qt handles them automatically.

    During development, the directory for plugins is \c{QTDIR/plugins}
    (where \c QTDIR is the directory where Qt is installed), with each
    type of plugin in a subdirectory for that type, for example, \c styles. If
    you want your applications to use plugins and you don't want to use
    the standard plugins path, have your installation process
    determine the path you want to use for the plugins, and save the
    path, for example, by using QSettings, for the application to read when it
    runs. The application can then call QCoreApplication::addLibraryPath()
    with this path and your plugins will be available to the application.
    Note that the final part of the path (for example, \c styles) cannot be changed.

    If you want the plugin to be loadable, one approach is to
    create a subdirectory under the application, and place the plugin
    in that directory. If you distribute any of the plugins that come
    with Qt (the ones located in the \c plugins directory), you must
    copy the subdirectory under \c plugins where the plugin is
    located to your applications root folder (i.e., do not include the
    \c plugins directory).

    For more information about deployment,
    see the \l {Deploying Qt Applications} and \l {Deploying Plugins}
    documentation.

    \section1 Static Plugins

    The normal and most flexible way to include a plugin with an
    application is to compile it into a dynamic library that is shipped
    separately, and detected and loaded at runtime.

    Plugins can be linked statically into your application. If you
    build the static version of Qt, this is the only option for
    including Qt's predefined plugins. Using static plugins makes the
    deployment less error-prone, but has the disadvantage that no
    functionality from plugins can be added without a complete rebuild
    and redistribution of the application.

    CMake and qmake automatically add the plugins that are typically needed by
    the Qt modules that are used, while more specialized plugins need to be
    added manually. The default list of automatically added plugins can be
    overridden per type.

    The defaults are tuned towards an optimal out-of-the-box experience, but may
    unnecessarily bloat the application. It is recommended to inspect the linker
    command line and eliminate unnecessary plugins.

    To cause static plugins actually being linked and instantiated,
    Q_IMPORT_PLUGIN() macros are also needed in application code, but those are
    automatically generated by the build system and added to your application
    project.

    \section2 Importing Static Plugins in CMake

    To statically link plugins in a CMake project, you need to call the
    \l{qt6_import_plugins}{qt_import_plugins} command.

    For example, the Linux \c libinput plugin is not imported by default. The
    following command imports it:

    \snippet plugins/doc_src_plugins-howto.cmake import_plugins

    To link the minimal platform integration plugin instead of the default Qt
    platform adaptation plugin, use:

    \snippet plugins/doc_src_plugins-howto.cmake import_minimal_plugin

    Another typical use case is to link only a certain set of \c imageformats
    plugins:

    \snippet plugins/doc_src_plugins-howto.cmake some_imageformat_plugins

    If you want to prevent the linking of any \c imageformats plugin, use:

    \snippet plugins/doc_src_plugins-howto.cmake no_imageformats_plugins

    If you want to turn off the addition of any default plugin, use the \c
    NO_DEFAULT option of \l{qt6_import_plugins}{qt_import_plugins}.

    \section2 Importing Static Plugins in qmake

    In a qmake project, you need to add the required plugins to your build using
    \c{QTPLUGIN}:

    \snippet plugins/doc_src_plugins-howto.pro 5

    For example, to link the minimal plugin instead of the default Qt
    platform adaptation plugin, use:

    \snippet plugins/doc_src_plugins-howto.pro 4

    If you want neither the default, nor the minimal QPA plugin to be
    linked automatically, use:

    \snippet plugins/doc_src_plugins-howto.pro 6

    If you do not want all plugins added to QTPLUGIN to be automatically
    linked, remove \c import_plugins from the \c CONFIG variable:

    \snippet plugins/doc_src_plugins-howto.pro 7

    \section2 Creating Static Plugins

    It is also possible to create your own static plugins by
    following these steps:

    \list 1
    \li Pass the \c STATIC option to the \l{qt6_add_plugin}{qt_add_plugin}
        command in your \c{CMakeLists.txt}. For a qmake project, add \c{CONFIG
        += static} to your plugin's \c .pro file.
    \li Use the Q_IMPORT_PLUGIN() macro in your application.
    \li Use the Q_INIT_RESOURCE() macro in your application if the plugin ships
       qrc files.
    \li Link your application with your plugin library using \l{CMake
        target_link_libraries Documentation}{target_link_libraries} in your
        \c{CMakeLists.txt} or \c LIBS in your \c .pro file.
    \endlist

    See the \l{tools/plugandpaint/app}{Plug & Paint} example and the
    associated \l{tools/plugandpaint/plugins/basictools}{Basic Tools}
    plugin for details on how to do this.

    \note If you are not using CMake or qmake to build your plugin, you need to
    make sure that the \c{QT_STATICPLUGIN} preprocessor macro is defined.

    \section2 Loading plugins

    Plugin types (static or shared) and operating systems require specific
    approaches to locate and load plugins. It's useful to implement an abstraction
    for loading plugins.

    \snippet demos/documentviewer/app/viewerfactory.cpp loader

    QPluginLoader::staticInstances() returns a QObjectList with a pointer to each
    statically linked plugin


    \snippet demos/documentviewer/app/viewerfactory.cpp static

    Shared plugins reside in their deployment directories, which may require
    OS-specific handling.

    \snippet demos/documentviewer/app/viewerfactory.cpp shared

    \section1 Deploying and Debugging Plugins

    The \l{Deploying Plugins} document covers the process of deploying
    plugins with applications and debugging them when problems arise.

    \sa QPluginLoader, QLibrary
*/