查看完整版本: [-- zhengtianzuo系列-Qt调用插件 --]

QTCN开发网 -> Qt代码秀 -> zhengtianzuo系列-Qt调用插件 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

zhengtianzuo 2017-10-10 16:03

zhengtianzuo系列-Qt调用插件

首先创建一个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


ccazqyy 2017-10-11 08:41
          

liuchangyin 2017-10-11 09:55

stlcours 2017-10-16 21:20
潜水员出来 顶贴 。内容不错

big_mouse 2020-04-22 09:15


查看完整版本: [-- zhengtianzuo系列-Qt调用插件 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled