How to Create Qt Plugins▲
Qt provides two APIs for creating plugins:
-
A high-level API for writing extensions to Qt itself, such as custom database drivers, image formats, text codecs, and custom styles.
-
A low-level API for extending Qt applications.
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 Creating Custom Widget Plugins.
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.
Base Class |
Directory Name |
Qt Module |
Key Case Sensitivity |
---|---|---|---|
QAccessibleBridgePlugin |
accessiblebridge |
Case Sensitive |
|
imageformats |
Case Sensitive |
||
QPictureFormatPlugin (obsolete) |
pictureformats |
Case Sensitive |
|
QBearerEnginePlugin |
bearer |
Case Sensitive |
|
QPlatformInputContextPlugin |
platforminputcontexts |
Case Insensitive |
|
QPlatformIntegrationPlugin |
platforms |
Case Insensitive |
|
QPlatformThemePlugin |
platformthemes |
Case Insensitive |
|
QPlatformPrinterSupportPlugin |
printsupport |
Case Insensitive |
|
QSGContextPlugin |
scenegraph |
Case Sensitive |
|
sqldrivers |
Case Sensitive |
||
iconengines |
Case Insensitive |
||
accessible |
Case Sensitive |
||
styles |
Case Insensitive |
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 &
amp;key);
}
;
Ensure that the class implementation is located in a .cpp file:
#include
"mystyleplugin.h"
QStyle *
MyStylePlugin::
create(const
QString &
amp;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. 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.
The Style Plugin Example shows how to implement a plugin that extends the QStylePlugin base class.
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:
-
Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.
-
Use the Q_DECLARE_INTERFACE() macro to tell Qt's meta-object system about the interface.
-
Use QPluginLoader in the application to load the plugins.
-
Use qobject_cast() to test whether a plugin implements a given interface.
Writing a plugin involves these steps:
-
Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.
-
Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.
-
Export the plugin using the Q_PLUGIN_METADATA() macro.
-
Build the plugin using a suitable .pro file.
For example, here's the definition of an interface class:
class
FilterInterface
{
public
:<