首先创建一个Qt的一般的动态库
然后创建一个
插件接口
文件, 如下:
```
#pragma once
#include <QtPlugin>
class CQtMyPluginInterface
{
public:
virtual ~CQtMyPluginInterface() {}
virtual
QString getString() = 0;
};
Q_DECLARE_INTERFACE(CQtMyPluginInterface, "cn.camelsoft.CQtMyPluginInterface/1.0")
```
1. ` #include <QtPlugin>` //引用Qt插件
系统头文件2. `Q_DECLARE_INTERFACE(CQtMyPluginInterface, "cn.camelsoft.CQtMyPluginInterface/1.0")` //申明接口
然后改造库类的头文件, 如下:
```
#pragma once
#include <QObject>
#include "QtMyPluginInterface.h"
class CQtMyPlugin : public
QObject, public CQtMyPluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA ( IID "cn.camelsoft.CQtMyPluginInterface/1.0")
Q_INTERFACES(CQtMyPluginInterface)
public:
virtual QString getString();
};
```
1. `Q_PLUGIN_METADATA ( IID "cn.camelsoft.CQtMyPluginInterface/1.0")` //注册插件ID
2. `Q_INTERFACES(CQtMyPluginInterface)` //申明插件接口
这样一个Qt插件框架下
开发的插件就做好了. 接下来是调用
```
QPluginLoader loader("QtMyPlugin.dll");
if (!loader.load())
{
return;
}
CQtMyPluginInterface *plugin = qobject_cast<CQtMyPluginInterface *>(loader.instance());
if (plugin)
{
QMessageBox::about(NULL, QStringLiteral("显示插件信息"), plugin->getString());
}
```
需要完整代码请访问
QtWidgetsExamples