KPluginFactory Class Reference
from PyKDE5.kcoreaddons import *
Inherits: QObject
Detailed Description
\class KPluginFactory kpluginfactory.h <KPluginFactory>
KPluginFactory provides a convenient way to provide factory-style plugins. Qt plugins provide a singleton object, but a common pattern is for plugins to generate as many objects of a particular type as the application requires. By using KPluginFactory, you can avoid implementing the factory pattern yourself.
KPluginFactory also allows plugins to provide multiple different object types, indexed by keywords.
The objects created by KPluginFactory must inherit QObject, and must have a standard constructor pattern:
T(QWidget *parentWidget, QObject *parent, const QVariantList &args)
T(QWidget *parent, const QVariantList &args)
T(QObject *parent, const QVariantList &args)
You should typically use either K_PLUGIN_FACTORY() or K_PLUGIN_FACTORY_WITH_JSON() in your plugin code to create the factory. The typical pattern is
#include <KPluginFactory> #include <plugininterface.h> class MyPlugin : public PluginInterface { public: MyPlugin(QObject *parent, const QVariantList &args) : PluginInterface(parent) {} }; K_PLUGIN_FACTORY(MyPluginFactory, registerPlugin<MyPlugin>(); ) #include <myplugin.moc>
If you want to write a custom KPluginFactory not using the standard macro(s) you can reimplement the create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword) method.
Example:
class SomeScriptLanguageFactory : public KPluginFactory { Q_OBJECT public: SomeScriptLanguageFactory() {} protected: virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword) { const QString identifier = QLatin1String(iface) + QLatin1Char('_') + keyword; // load scripting language module from the information in identifier // and return it: return object; } };
If you want to load a library use KPluginLoader. The application that wants to instantiate plugin classes can do the following:
KPluginFactory *factory = KPluginLoader("libraryname").factory(); if (factory) { PluginInterface *p1 = factory->create<PluginInterface>(parent); OtherInterface *p2 = factory->create<OtherInterface>(parent); NextInterface *p3 = factory->create<NextInterface>("keyword1", parent); NextInterface *p3 = factory->create<NextInterface>("keyword2", parent); }
Signals | |
objectCreated (QObject object) | |
Methods | |
__init__ (self) | |
Static Methods | |
[QVariant] | stringListToVariantList (QStringList list) |
QStringList | variantListToStringList ([QVariant] list) |
Signal Documentation
objectCreated | ( | QObject | object | |
) |
- Signal syntax:
QObject.connect(source, SIGNAL("objectCreated(QObject*)"), target_slot)
Method Documentation
__init__ | ( | self ) |
This constructor creates a factory for a plugin.
Static Method Documentation
[QVariant] stringListToVariantList | ( | QStringList | list | |
) |
- Internal:
- Converts a QStringList to a QVariantList
QStringList variantListToStringList | ( | [QVariant] | list | |
) |
- Internal:
- Converts a QVariantList of strings to a QStringList