16,235
社区成员
发帖
与我相关
我的任务
分享
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;
}
[/code=c]
Q_EXPORT_PLUGIN2(pnp_mystyleplugin, 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.
The Style Plugin Example shows how to implement a plugin that extends the QStylePlugin base class.
The Lower-Level API: Extending Qt Applications
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:
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_EXPORT_PLUGIN2() macro.
Build the plugin using a suitable .pro file.
For example, here's the definition of an interface class:
[code=c]
class FilterInterface
{
public:
virtual ~FilterInterface() {}
virtual QStringList filters() const = 0;
virtual QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent) = 0;
};
[/code=c]
Here's the definition of a plugin class that implements that interface:
[code=c]
#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);
};