#include <QString>
#include <QtPlugin>
#include <QObject>
struct AddonInfo
{
QString name;
QString description;
QString version;
QString author;
};
class AddonInterface : public QObject
{
Q_OBJECT
public:
virtual ~AddonInterface() {}
virtual AddonInfo addonInfo()const { return AddonInfo(); }
};
Q_DECLARE_INTERFACE(AddonInterface,"com.ppx.addon.interface/1.0")
class AddonInterfaceIMPL : public AddonInterface
{
Q_OBJECT
public:
AddonInfo addonInfo()const;
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "com.ppx.addon.interface")
Q_INTERFACES(AddonInterface);
#endif
};
#include "addon.h"
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(FilterInterface,plugin)
#endif // QT_VERSION < 0x050000
AddonInfo AddonInterfaceIMPL::addonInfo()const
{
AddonInfo addon;
addon.name = "MyAddOn";
addon.author = "Coder";
addon.description = "Description";
addon.version = "1.0";
return addon;
}
使用
#include <qplugin.h>
#include <qpluginloader.h>
#include <qdebug>
#include "addon.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPluginLoader loader("addon.dll");
qDebug()<<"load plugin :"<<loader.load();
auto object = loader.instance();
qDebug() << "object:" << object;
if (object)
{
AddonInterface* face = qobject_cast<AddonInterface*>(object);
qDebug() << "face ptr:"<<face;
auto info = face->addonInfo();
qDebug() << info.author << " " << info.description << " " << info.name << " " << info.version;
}
return a.exec();
}