QT插件简单使用

目录

总的目录结构

1 主程序

2 插件程序

3 运行结果

 4 demo压缩包


总的目录结构

        编译器 mingw 64-bit

 

1 主程序

1 新建一个其他项目的子文件目录后,创建一个普通的 QMainWindow 项目 

主程序中添加头文件  Interface.h 内容如下:

#ifndef INTERFACE_H
#define INTERFACE_H
#include <QObject>
#include <QtPlugin>
//定义接口
class Interface
{
public:
    virtual ~Interface() {}
    virtual int add(int a,int b) = 0;
};

QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(Interface, "org.qt-project.Qt.QGenericPluginFactoryInterface")
QT_END_NAMESPACE

#endif // INTERFACE_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "interface.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void LoadPlugin();
    Interface* m_pInterface = nullptr;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPluginLoader>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    LoadPlugin();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::LoadPlugin()
{
    QString filename = qApp->applicationDirPath() + "/Plugin.dll";
    qDebug()<<"LoadPlugin filename:"<<filename;
    QPluginLoader pluginLoader(filename);
    QObject *plugin = pluginLoader.instance();

    if (plugin) {
        m_pInterface = qobject_cast<Interface *>(plugin);
        if (m_pInterface)
            qDebug()<<"LoadPlugin success! 7 + 8 = "<<m_pInterface->add(7,8);
        else
            qDebug()<<"qobject_cast fail!";
    }
    else
        qDebug()<<"LoadPlugin fail!";

}

2 插件程序

1 右击主项目 新建子项目项

(也可以新建另一个工程 ) 

选择C++ Library ---->QT Plugin

 

将 Interface.h拷贝到该目录下 与Plugin.pro同级目录

genericplugin.h
#ifndef GENERICPLUGIN_H
#define GENERICPLUGIN_H

#include <QGenericPlugin>
#include "Interface.h"
class GenericPlugin : public QGenericPlugin, public Interface
{
    Q_OBJECT
    Q_INTERFACES(Interface)
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QGenericPluginFactoryInterface/1.0" FILE "Plugin.json")

public:
    explicit GenericPlugin(QObject *parent = nullptr);
    int add(int a,int b);
private:
    QObject *create(const QString &name, const QString &spec) override;
};

#endif // GENERICPLUGIN_H
genericplugin.cpp
#include "genericplugin.h"

GenericPlugin::GenericPlugin(QObject *parent)
    : QGenericPlugin(parent)
{
}

int GenericPlugin::add(int a, int b)
{
    return a + b;
}

QObject *GenericPlugin::create(const QString &name, const QString &spec)
{
    //static_assert(false, "You need to implement this function");

    return nullptr;
}

Plugin.json

{
    "Keys" : [ ]
}
3 运行结果

将生成的Plugin.dll 和 libPlugin.a拷贝到 exe 同级目录,运行结果如下:

 4 demo压缩包

https://download.csdn.net/download/weixin_44270564/88059891?spm=1001.2014.3001.5503

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值