Qt provides two APIs for creating plugins:
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 Qt Designer, see the Qt Designer module documentation.
话题:
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.
基类 | 目录名称 | Qt 模块 | 键区分大小写 |
---|---|---|---|
QAccessibleBridgePlugin |
accessiblebridge
|
Qt GUI | 区分大小写 |
QImageIOPlugin |
imageformats
|
Qt GUI | 区分大小写 |
QPictureFormatPlugin (obsolete) |
pictureformats
|
Qt GUI | 区分大小写 |
QAudioSystemPlugin |
audio
|
Qt Multimedia | 不区分大小写 |
QDeclarativeVideoBackendFactoryInterface |
video/declarativevideobackend
|
Qt Multimedia | 不区分大小写 |
QGstBufferPoolPlugin |
video/bufferpool
|
Qt Multimedia | 不区分大小写 |
QMediaPlaylistIOPlugin |
playlistformats
|
Qt Multimedia | 不区分大小写 |
QMediaResourcePolicyPlugin |
resourcepolicy
|
Qt Multimedia | 不区分大小写 |
QMediaServiceProviderPlugin |
mediaservice
|
Qt Multimedia | 不区分大小写 |
QSGVideoNodeFactoryPlugin |
video/videonode
|
Qt Multimedia | 不区分大小写 |
QBearerEnginePlugin |
bearer
|
Qt Network | 区分大小写 |
QPlatformInputContextPlugin |
platforminputcontexts
|
Qt 平台抽象 | 不区分大小写 |
QPlatformIntegrationPlugin |
platforms
|
Qt 平台抽象 | 不区分大小写 |
QPlatformThemePlugin |
platformthemes
|
Qt 平台抽象 | 不区分大小写 |
QGeoPositionInfoSourceFactory |
position
|
Qt Positioning | 区分大小写 |
QPlatformPrinterSupportPlugin |
printsupport
|
Qt Print Support | 不区分大小写 |
QSGContextPlugin |
scenegraph
|
Qt Quick | 区分大小写 |
QScriptExtensionPlugin |
script
|
Qt Script | 区分大小写 |
QSensorGesturePluginInterface |
sensorgestures
|
Qt Sensors | 区分大小写 |
QSensorPluginInterface |
sensors
|
Qt Sensors | 区分大小写 |
QSqlDriverPlugin |
sqldrivers
|
Qt SQL | 区分大小写 |
QIconEnginePlugin |
iconengines
|
Qt SVG | 不区分大小写 |
QAccessiblePlugin |
accessible
|
Qt Widgets | 区分大小写 |
QStylePlugin |
styles
|
Qt Widgets | 不区分大小写 |
If you have a new style class called
MyStyle
that you want to make available as a plugin, the class needs to be defined as follows (
mystyleplugin.h
):
class MyStylePlugin : public QStylePlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyleplugin.json") public: QStyle *create(const QString &key); };
Ensure that the class implementation is located in a
.cpp
文件:
#include "mystyleplugin.h" QStyle *MyStylePlugin::create(const QString &key) { if (key.toLower() == "mystyle") return new MyStyle; return 0; }
(Note that QStylePlugin is case-insensitive, and the lowercase version of the key is used in our create() implementation; most other plugins are case sensitive.)
In addition, a json file (
mystyleplugin.json
) containing meta data describing the plugin is required for most plugins. For style plugins it simply contains a list of styles that can be created by the plugin:
{ "Keys": [ "mystyleplugin" ] }
The type of information that needs to be provided in the json file is plugin dependent, please 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. Styles are an exception, since you might want to set a style explicitly in code. To apply a style, use code like this:
QApplication::setStyle(QStyleFactory::create("MyStyle"));
Some plugin classes 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.
Style Plugin Example shows how to implement a plugin that extends the QStylePlugin base class.
Not only Qt itself but also Qt application 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 the other types of plugin that extend Qt's functionality.
Making an application extensible through plugins involves the following steps:
Writing a plugin involves these steps:
.pro
文件。
For example, here's the definition of an interface class:
class FilterInterface { public: virtual ~FilterInterface() {} virtual QStringList filters() const = 0; virtual QImage filterImage(const QString &filter, const QImage &image, QWidget *parent) = 0; };
Here's the definition of a plugin class that implements that interface:
#include <QObject> #include <QtPlugin> #include <QStringList> #include <QImage> #include <plugandpaint/interfaces.h> class ExtraFiltersPlugin : public QObject, public FilterInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" FILE "extrafilters.json") Q_INTERFACES(FilterInterface) public: QStringList filters() const; QImage filterImage(const QString &filter, const QImage &image, QWidget *parent); };
插件和描绘 example documentation explains this process in detail. See also 创建自定义 Widget 为 Qt Designer for information about issues that are specific to Qt Designer. You can also take a look at the 回显插件范例 which is a more trivial example on how to implement a plugin that extends Qt applications. Please note that a QCoreApplication must have been initialized before plugins can be loaded.
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
QTDIR/plugins
(where
QTDIR
is the directory where Qt is installed), with each type of plugin in a subdirectory for that type, for example,
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,
styles
) cannot be changed.
If you want the plugin to be loadable then 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
plugins
directory), you must copy the subdirectory under
plugins
where the plugin is located to your applications root folder (i.e., do not include the
plugins
目录)。
For more information about deployment, see the 部署 Qt 应用程序 and 部署插件 文档编制。
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.
To link plugins statically, you need to add the required plugins to your build using
QTPLUGIN
.
In the
.pro
file for your application, you need the following entry:
QTPLUGIN += qjpeg \ qgif \ qkrcodecs
qmake automatically adds the plugins to QTPLUGIN that are typically needed by the Qt modules used (see QT ), while more specialized plugins need to be added manually. The default list of automatically added plugins can be overridden per type. For example, to link the minimal plugin instead of the default Qt platform adaptation plugin, use:
QTPLUGIN.platforms = qminimal
If you want neither the default, nor the minimal QPA plugin to be linked automatically, use:
QTPLUGIN.platforms = -
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 built by qmake 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 qmake and added to your application project.
If you do not want all plugins added to QTPLUGIN to be automatically linked, remove
import_plugins
从
CONFIG
变量:
CONFIG -= import_plugins
It is also possible to create your own static plugins, by following these steps:
CONFIG += static
to your plugin's
.pro
文件。
LIBS
在
.pro
文件。
见 插件和描绘 example and the associated 基本工具 plugin for details on how to do this.
注意:
If you are not using qmake to build your plugin you need to make sure that the
QT_STATICPLUGIN
preprocessor macro is defined.
部署插件 document covers the process of deploying plugins with applications and debugging them when problems arise.
另请参阅 QPluginLoader , QLibrary ,和 插件和描绘范例 .