Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |
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 QtDesigner module documentation.
Topics:
Writing a plugin that extends Qt itself is achieved by subclassing the appropriate plugin base clase, implementing a few functions, and adding a macro.
There are several plugin base classes. Derived plugins are stored by default in the standard plugin directory.
Base Class | Default Path | Key Case Sensitivity |
---|---|---|
QAccessibleBridgePlugin | plugins/accessiblebridge | Case Sensitive |
QAccessiblePlugin | plugins/accessible | Case Sensitive |
QDecorationPlugin | plugins/decorations | Case Sensitive |
QIconEnginePlugin | plugins/iconengines | Case Insensitive |
QImageIOPlugin | plugins/imageformats | Case Sensitive |
QInputContextPlugin | plugins/inputmethods | Case Sensitive |
QKbdDriverPlugin | plugins/kbddrivers | Case Sensitive |
QMouseDriverPlugin | plugins/mousedrivers | Case Sensitive |
QPictureFormatPlugin | plugins/pictureformats | Case Sensitive |
QScreenDriverPlugin | plugins/gfxdrivers | Case Sensitive |
QSqlDriverPlugin | plugins/sqldrivers | Case Sensitive |
QStylePlugin | plugins/styles | Case Insensitive |
QTextCodecPlugin | plugins/codecs | Case Sensitive |
But where is the plugins directory? When the application is run, Qt will first treat the application's executable directory as the pluginsbase. For example if the application is in C:\Program Files\MyApp and has a style plugin, Qt will look in C:\Program Files\MyApp\styles. (See QCoreApplication::applicationDirPath() for how to find out where the application's executable is.) Qt will also look in the directory specified by QLibraryInfo::location(QLibraryInfo::PluginsPath), which typically is located in QTDIR/plugins (where QTDIR is the directory where Qt is installed). If you want Qt to look in additional places you can add as many paths as you need with calls to QCoreApplication::addLibraryPath(). And if you want to set your own path or paths you can use QCoreApplication::setLibraryPaths(). You can also use a qt.conf file to override the hard-coded paths that are compiled into the Qt library. For more information, see the Using qt.conf documentation.
Suppose that you have a new style class called MyStyle that you want to make available as a plugin. The required code is straightforward, here is the class definition (mystyleplugin.h):
class MyStylePlugin : public QStylePlugin { public: QStringList keys() const; QStyle *create(const QString &key); };
Ensure that the class implementation is located in a .cpp file (including the class definition):
#include "mystyleplugin.h" QStringList MyStylePlugin::keys() const { return QStringList() << "MyStyle"; } QStyle *MyStylePlugin::create(const QString &key) { if (key.toLower() == "mystyle") return new MyStyle; return 0; } Q_EXPORT_PLUGIN2(MyStylePlugin)
(Note that QStylePlugin is case insensitive, and the lower-case version of the key is used in our create() 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.
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.
The default 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, 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. 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 (e.g., styles) cannot be changed.
The normal way to include a plugin with an application is either to compile it in with the application or to compile it into a dynamic library and use it like any other library. 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. For more information about deployment, see the Deploying Qt Applications documentation.
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:
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; }; Q_DECLARE_INTERFACE(FilterInterface, "com.trolltech.PlugAndPaint.FilterInterface/1.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 : public QObject, public FilterInterface { Q_OBJECT Q_INTERFACES(FilterInterface) public: QStringList filters() const; QImage filterImage(const QString &filter, const QImage &image, QWidget *parent); };
The Plug & Paint example documentation explains this process in detail. See also Creating Custom Widgets for Qt Designer for information about issues that are specific to Qt Designer.
When loading plugins, the Qt library does some sanity checking to determine whether or not the plugin can be loaded and used. This provides the ability to have multiple versions and configurations of the Qt library installed side by side.
Rationale: A plugin linked against a newer Qt library may use new features that are not available in older versions. Trolltech has a policy of adding new features and APIs only between minor releases, which is why this test only looks at the major and minor version numbers, and not at the patch version number.
Rationale: See the Rationale for the build key below.
When building plugins to extend an application, it is important to ensure that the plugin is configured in the same way as the application. This means that if the application was built in release mode, plugins should be built in release mode, too.
If you configure Qt to be built in both debug and release modes, but only build applications in release mode, you need to ensure that your plugins are also built in release mode. By default, if a debug build of Qt is available, plugins will only be built in debug mode. To force the plugins to be built in release mode, add the following line to the plugin's project file:
CONFIG += release
This will ensure that the plugin is compatible with the version of the library used in the application.
When loading plugins, Qt checks the build key of each plugin against its own configuration to ensure that only compatible plugins are loaded; any plugins that are configured differently are not loaded.
The build key contains the following information:
Rationale: In cases where different versions of the same compiler do not produce binary compatible code, the version of the compiler is also present in the build key.
Rationale: Two different configurations of the same version of the Qt library are not binary compatible. The Qt library that loads the plugin uses the list of (missing) features to determine if the plugin is binary compatible.
Note: There are cases where a plugin can use features that are available in two different configurations. However, the developer writing plugins would need to know which features are in use, both in their plugin and internally by the utility classes in Qt. The Qt library would require complex feature and dependency queries and verification when loading plugins. Requiring this would place an unnecessary burden on the developer, and increase the overhead of loading a plugin. To reduce both development time and application runtime costs, a simple string comparision of the build keys is used.
Rationale: When distributing binaries of the Qt library with an application, this provides a way for developers to write plugins that can only be loaded by the library with which the plugins were linked.
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.
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 (Qtopia) | Default style |
qdecorationwindows | Decorations (Qtopia) | Windows style |
qgif | Image formats | GIF |
qjpeg | Image formats | JPEG |
qmng | Image formats | MNG |
qimsw_multi | Input methods (Qtopia) | Input Method Switcher |
qwstslibmousehandler | Mouse drivers (Qtopia) | tslib mouse |
qgfxtransformed | Graphic drivers (Qtopia) | Transformed screen |
qgfxvnc | Graphic drivers (Qtopia) | VNC |
qscreenvfb | Graphic drivers (Qtopia) | 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 the Q_IMPORT_PLUGIN() macro in your application and you need to add the required plugins to your build using QTPLUGIN. For example, in your main.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(); }
TEMPLATE = app QTPLUGIN += qjpeg qgif # image formats QTPLUGIN += qkrcodecs # text codecs
It is also possible to create your own static plugins, by following these steps:
See also QPluginLoader and QLibrary.
Copyright © 2006 Trolltech | Trademarks | Qt 4.1.3 |