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

Qt GUI

Case Sensitive

QImageIOPlugin

imageformats

Qt GUI

Case Sensitive

QPictureFormatPlugin (obsolete)

pictureformats

Qt GUI

Case Sensitive

QBearerEnginePlugin

bearer

Qt Network

Case Sensitive

QPlatformInputContextPlugin

platforminputcontexts

Qt Platform Abstraction

Case Insensitive

QPlatformIntegrationPlugin

platforms

Qt Platform Abstraction

Case Insensitive

QPlatformThemePlugin

platformthemes

Qt Platform Abstraction

Case Insensitive

QPlatformPrinterSupportPlugin

printsupport

Qt Print Support

Case Insensitive

QSGContextPlugin

scenegraph

Qt Quick

Case Sensitive

QSqlDriverPlugin

sqldrivers

Qt SQL

Case Sensitive

QIconEnginePlugin

iconengines

Qt SVG

Case Insensitive

QAccessiblePlugin

accessible

Qt Widgets

Case Sensitive

QStylePlugin

styles

Qt Widgets

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):

 
Sélectionnez
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 file:

 
Sélectionnez
#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:

 
Sélectionnez
{ "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:

 
Sélectionnez
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:

  1. Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.

  2. Use the Q_DECLARE_INTERFACE() macro to tell Qt's meta-object system about the interface.

  3. Use QPluginLoader in the application to load the plugins.

  4. Use qobject_cast() to test whether a plugin implements a given interface.

Writing a plugin involves these steps:

  1. Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.

  2. Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.

  3. Export the plugin using the Q_PLUGIN_METADATA() macro.

  4. Build the plugin using a suitable .pro file.

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

 
Sélectionnez
class FilterInterface
{
public:<