
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
Qt provides two APIs for creating plugins:
For example, if you want to write a customQStyle 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 withQt Designer, see theQtDesigner module documentation.
Topics:
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 right directory.
| Base Class | Directory Name | Key Case Sensitivity |
|---|---|---|
| QAccessibleBridgePlugin | accessiblebridge | Case Sensitive |
| QAccessiblePlugin | accessible | Case Sensitive |
| QDecorationPlugin | decorations | Case Insensitive |
| QFontEnginePlugin | fontengines | Case Insensitive |
| QIconEnginePlugin | iconengines | Case Insensitive |
| QImageIOPlugin | imageformats | Case Sensitive |
| QInputContextPlugin | inputmethods | Case Sensitive |
| QKbdDriverPlugin | kbddrivers | Case Insensitive |
| QMouseDriverPlugin | mousedrivers | Case Insensitive |
| QScreenDriverPlugin | gfxdrivers | Case Insensitive |
| QScriptExtensionPlugin | script | Case Sensitive |
| QSqlDriverPlugin | sqldrivers | Case Sensitive |
| QStylePlugin | styles | Case Insensitive |
| QTextCodecPlugin | codecs | Case Sensitive |
Suppose that you have a new style class calledMyStyle that you want to make available as a plugin. The required code is straightforward, here is the class definition (mystyleplugin.h):
class MyStylePlugin :publicQStylePlugin{public:QStringList keys()const;QStyle*create(constQString&key);};
Ensure that the class implementation is located in a.cpp file (including the class definition):
#include "mystyleplugin.h"QStringList MyStylePlugin::keys()const{returnQStringList()<<"MyStyle";}QStyle*MyStylePlugin::create(constQString&key){if (key.toLower()=="mystyle")returnnew MyStyle;return0;}Q_EXPORT_PLUGIN2(pnp_mystyleplugin, MyStylePlugin)
(Note thatQStylePlugin is case insensitive, and the lower-case version of the key is used in ourcreate() implementation; most other plugins are case sensitive.)
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.
TheStyle Plugin Example shows how to implement a plugin that extends theQStylePlugin base class.
Not only Qt itself but also Qt application can be extended through plugins. This requires the application to detect and load plugins usingQPluginLoader. 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 file.For example, here's the definition of an interface class:
class FilterInterface{public:virtual~FilterInterface() {}virtualQStringList filters()const=0;virtualQImage filterImage(constQString&filter,constQImage&image,QWidget*parent)=0;};
Here's the definition of a plugin class that implements that interface:
#include <QObject>#include <QStringList>#include <QImage>#include <plugandpaint/interfaces.h>class ExtraFiltersPlugin :publicQObject,public FilterInterface{ Q_OBJECT Q_INTERFACES(FilterInterface)public:QStringList filters()const;QImage filterImage(constQString&filter,constQImage&image,QWidget*parent);};
ThePlug & Paint example documentation explains this process in detail. See alsoCreating Custom Widgets for Qt Designer for information about issues that are specific toQt Designer. You can also take a look at theEcho Plugin Example is a more trivial example on how to implement a plugin that extends Qt applications. Please note that aQCoreApplication 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 isQTDIR/plugins (whereQTDIR is the directory where Qt is installed), with each type of plugin in a subdirectory for that type, e.g.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, e.g. usingQSettings, for the application to read when it runs. The application can then callQCoreApplication::addLibraryPath() with this path and your plugins will be available to the application. Note that the final part of the path (e.g.,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 theplugins directory), you must copy the sub-directory underplugins where the plugin is located to your applications root folder (i.e., do not include theplugins directory).
Note:In Symbian all binaries must be located in the directory \sys\bin, so each Qt plugin has a stub with the same basename as the plugin dll and suffix ".qtplugin" to make Qt extension plugins work similarly to other platforms. When trying to locate the plugin, Qt actually looks for the stub instead of the plugin binary. While plugin stub files have the suffix ".qtplugin", they can still be loaded also by specifying a filename with the normal library suffix ".dll" forQPluginLoader, so normally application developer doesn't need to care about the different suffix of the stub. Because of the way applications can be installed on ROM or various other drives in Symbian, Qt looks for the stub from the same directory on all available drives if it is not located in the given directory when loading a plugin.
For more information about deployment, see theDeploying Qt Applications andDeploying Plugins documentation.
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 against 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.
When compiled as a static library, Qt provides the following static plugins:
| Plugin name | Type | Description |
|---|---|---|
qtaccessiblecompatwidgets | Accessibility | Accessibility for Qt 3 support widgets |
qtaccessiblewidgets | Accessibility | Accessibility for Qt widgets |
qdecorationdefault | Decorations (Qt Extended) | Default style |
qdecorationwindows | Decorations (Qt Extended) | Windows style |
qgif | Image formats | GIF |
qjpeg | Image formats | JPEG |
qmng | Image formats | MNG |
qico | Image formats | ICO |
qsvg | Image formats | SVG |
qtiff | Image formats | TIFF |
qimsw_multi | Input methods (Qt Extended) | Input Method Switcher |
qwstslibmousehandler | Mouse drivers (Qt Extended) | tslib mouse |
qgfxtransformed | Graphic drivers (Qt Extended) | Transformed screen |
qgfxvnc | Graphic drivers (Qt Extended) | VNC |
qscreenvfb | Graphic drivers (Qt Extended) | Virtual frame buffer |
qsqldb2 | SQL driver | IBM DB2 |
qsqlibase | SQL driver | Borland InterBase |
qsqlite | SQL driver | SQLite version 3 |
qsqlite2 | SQL driver | SQLite version 2 |
qsqlmysql | SQL driver | MySQL |
qsqloci | SQL driver | Oracle (OCI) |
qsqlodbc | SQL driver | Open Database Connectivity (ODBC) |
qsqlpsql | SQL driver | PostgreSQL |
qsqltds | SQL driver | Sybase Adaptive Server (TDS) |
qcncodecs | Text codecs | Simplified Chinese (People's Republic of China) |
qjpcodecs | Text codecs | Japanese |
qkrcodecs | Text codecs | Korean |
qtwcodecs | Text codecs | Traditional Chinese (Taiwan) |
To link statically against those plugins, you need to use theQ_IMPORT_PLUGIN() macro in your application and you need to add the required plugins to your build usingQTPLUGIN. For example, in yourmain.cpp:
#include <QApplication>#include <QtPlugin>Q_IMPORT_PLUGIN(qjpeg)Q_IMPORT_PLUGIN(qgif)Q_IMPORT_PLUGIN(qkrcodecs)int main(int argc,char*argv[]){QApplication app(argc, argv);...return app.exec();}
In the.pro file for your application, you need the following entry:
QTPLUGIN += qjpeg \ qgif \ qkrcodecs
It is also possible to create your own static plugins, by following these steps:
CONFIG += static to your plugin's.pro file.LIBS in the.pro file.See thePlug & Paint example and the associatedBasic Tools plugin for details on how to do this.
Note:If you are not using qmake to build your application you need to make sure that theQT_STATICPLUGIN preprocessor macro is defined.
TheDeploying Plugins document covers the process of deploying plugins with applications and debugging them when problems arise.
See alsoQPluginLoader,QLibrary, andPlug & Paint Example.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.