基于QPluginLoader的简单插件系统

本文介绍了如何使用Qt的QPluginLoader实现插件系统,包括定义纯虚类、创建插件类以及主程序加载插件的过程。示例代码展示了如何创建一个简单的插件并进行功能调用,实现了动态加载和使用插件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插件系统分为两部分
	第一部分:主程序,实现系统的 通用逻辑,加载定制化的插件
	第二部分:插件,实现定制化的功能。
系统的升级即可通过升级不同的插件即可完成整个系统的升级

Qt插件系统中 我们使用 QPluginLoader 这个类去实现,主系统自动化加载插件。

1.定义好系统的纯虚类
eg:

#ifndef FUNABSTRSCT_H
#define FUNABSTRSCT_H

#include <QObject>

class FunAbstrsct
{
public:
    virtual void print(const QString &str) = 0;				//功能1
    virtual int read(const char *buf,int size) = 0;			//功能2
    virtual int write(const char *buf,int size) = 0;		//功能3
};
//注意在纯虚类里加入 Q_DECLARE_INTERFACE 这个宏
#define QtPluginDemo_iid "version_1.0"						//插件的id
Q_DECLARE_INTERFACE ( FunAbstrsct, QtPluginDemo_iid ) ;
#endif // FUNABSTRSCT_H

2.继承纯虚类,实现定制化功能
eg:

#ifndef UNTITLED_H
#define UNTITLED_H

#include "untitled_global.h"
//#include <QObject>
#include "../../main/mainTest/funabstrsct.h"

class UNTITLEDSHARED_EXPORT Untitled : public QObject,public FunAbstrsct
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QtPluginDemo_iid FILE "MyPlugin.json")
    Q_INTERFACES(FunAbstrsct)
public:
    Untitled(QObject *par = nullptr);
    virtual void print(const QString &str);
    virtual int read(const char *buf,int size);
    virtual int write(const char* buf,int size);
};

#endif // UNTITLED_H
#include "untitled.h"
#include <QDebug>
Untitled::Untitled(QObject *par) : QObject (par)
{

}

void Untitled::print(const QString &str)
{
    qDebug() << str;
}

int Untitled::read(const char *buf, int size)
{
    qDebug() << "read is run";
    return  0;
}

int Untitled::write(const char *buf, int size)
{
    qDebug() << "wrie is run";
    return 0;
}

3.实现主系统加载插件
eg:

#include "mainwindow.h"
#include <QApplication>
#include <QPluginLoader>
#include "../../mainTest/funabstrsct.h"
#include <QDebug>
#include <vector>
#include <QFile>
using namespace std;
vector<QPluginLoader*> m_loader;
vector<QString>       m_dllName;    //配置文件中存放dll的名称
vector<FunAbstrsct*>  m_fun;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    m_loader.clear();
    {
        QPluginLoader loader("./untitled.dll");
        m_loader.push_back(&loader);
        QObject *loaderPlugin = loader.instance();
        FunAbstrsct *pFunBase;
        if(!loader.isLoaded())
        {
            qDebug() << loader.errorString();
        }
        if(loaderPlugin)
        {
            pFunBase = qobject_cast<FunAbstrsct *>(loaderPlugin);
            m_fun.push_back(pFunBase);
            pFunBase->print("234");
            char *test = new char[10];
            pFunBase->read(test,10);
            pFunBase->write(test,10);
        }
    }
    m_fun.at(0)->print("123456");
    MainWindow w;
    w.show();
    return a.exec();
}

主程序运行结果

在这里插入图片描述

源代码下载地址
https://gitee.com/shijiaming-code/demo/tree/master/QtPlus

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值