QT Plugin 插件开发
创始人
2025-05-29 10:59:17
0

        插件是一种(遵循一定规范的应用程序接口编写出来的)程序,定位于开发实现应用软件平台不具备的功能的程序。
        插件必须依赖于应用程序才能发挥自身功能,仅靠插件是无法正常运行的;相反地,应用程序并不需要依赖插件就可以运行,这样一来,插件就可以加载到应用程序上并且动态更新而不会对应用诚信度造成任何改变(热更新)。

        插件就行硬件插卡一样,可以被随时删除、插入和修改,所以结构很灵活,容易修改,方便软件的升级和维护。
  

本文作者原创,转载请附上文章出处与本文链接。

QT 插件开发目录

1 新建QT项目

1.1 Pro文件

1.2 .h文件

1.3 .cpp文件

1.4 DeclareInterface.h虚函数文件

2 新建Plugin插件

 2.1 QPluginA.pro

2.2 qplugina.h

2.3 qplugina.cpp

2.4 DeclareInterface.h

3 效果

4 目录结构


1 新建QT项目

        新建QMainPlugin

 

1.1 Pro文件

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \DeclareInterface.h \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

1.2 .h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include #include 
#include 
#include 
#include #include "DeclareInterface.h"QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();private:Ui::MainWindow *ui;bool loadPlugin();   //加载插件DeclareInterface* m_pInterface = nullptr;  //获取插件类型
};
#endif // MAINWINDOW_H

1.3 .cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);if(!loadPlugin()){QMessageBox::warning(this, "Error", "Could not load the plugin");}}MainWindow::~MainWindow()
{delete ui;
}bool MainWindow::loadPlugin(){QPluginLoader pluginLoader("HelloCTK.dll");QObject *plugin = pluginLoader.instance();qDebug() << __FUNCTION__ << pluginLoader.errorString();if (plugin) {m_pInterface = qobject_cast(plugin);if (m_pInterface)return true;}return false;
}void MainWindow::on_pushButton_clicked()
{int a = 5;int b = 5;int r = -1;if(m_pInterface){r = m_pInterface->add(a, b);}QMessageBox::warning(this, "插件调用成功", QString::number(r));}

1.4 DeclareInterface.h虚函数文件

//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H#include //定义接口
class DeclareInterface
{
public:virtual ~DeclareInterface() {}virtual int add(int a,int b) = 0;
};//一定是唯一的标识符
#define DeclareInterface_iid "Examples.Plugin.DeclareInterface"QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeclareInterface, DeclareInterface_iid)
QT_END_NAMESPACE#endif // DECLAREINTERFACE_H

2 新建Plugin插件

        新建QPluginA项目

 2.1 QPluginA.pro

QT += core
QT += gui
QT             += widgetsTEMPLATE = lib
CONFIG += plugin
TARGET = HelloCTKCONFIG += c++11EXAMPLE_FILES = qtplugin.jsongreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \qplugina.cppHEADERS += \DeclareInterface.h \QPluginA_global.h \qplugina.h# Default rules for deployment.
unix {target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

2.2 qplugina.h

#ifndef QPLUGINA_H
#define QPLUGINA_H#include "QPluginA_global.h"
#include 
#include 
#include "DeclareInterface.h"class  QPluginA : public QObject, public DeclareInterface
{Q_OBJECTQ_INTERFACES(DeclareInterface)Q_PLUGIN_METADATA(IID  "qtplugin.json")public:QPluginA(QObject *parent = 0);int add(int a, int b);};#endif // QPLUGINA_H

2.3 qplugina.cpp

#include "qplugina.h"QPluginA::QPluginA(QObject *parent) : QObject(parent)
{
}int QPluginA::add(int a, int b)
{return a+b;
}

2.4 DeclareInterface.h

//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H#include //定义接口
class DeclareInterface
{
public:virtual ~DeclareInterface() {}virtual int add(int a,int b) = 0;
};//一定是唯一的标识符
#define DeclareInterface_iid "Examples.Plugin.DeclareInterface"QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeclareInterface, DeclareInterface_iid)
QT_END_NAMESPACE#endif // DECLAREINTERFACE_H

3 效果

4 目录结构

 

相关内容

热门资讯

技术布道 | 推动XR技术在产... 近年来,越来越多的企业正在利用扩展现实(XR)为用户提供沉...
5.docker入门到精通—安... **面试题:**1-2 亿条数据需要缓存,请问如何设计这个存储案例&#x...
渗透学习-CTF篇-web-C... 文章目录前言web入门部分反序列化web254web255web256web257web258 前...
Golang在ACM模式下的刷... 受之前用C和C++刷题的影响,所有输入我都喜欢用scanf处理,恰恰golang也有scanf函数,...
【计量经济学】【高教版】第二次... 第二次作业: 教材:伍德里奇。计量经济学导论:现代观点(第五版)。 第三章习题:必做 1,2,5,6...
开源时序数据库学习 计划学习使用QuestDB解决大数据日志存储场景。以下是常见引擎比较 比较项目 InfluxD...
OpenGL学习日志之深度测试 为什么需要深度缓冲区? 当绘制一个四边形的时候,由于我们绘制的时候是一个...
JVM笔记(五)垃圾收集算法 垃圾收集算法当前商业虚拟机的垃圾收集器,大多数都遵循了“分代收集”(Ge...
Windows安装部署ngin... Windows安装部署nginx 1、官网下载安装包: 官网地址:ngi...
解决win10任何程序打开链接... 文章目录一、问题与修改原因1、着手修改吧2、弯路上探索3、发现祸根二、后话 文章原出处:...
JS中的数组 系列文章目录 前端系列文章——传送门 JavaScript系列文章——传送门 文章目录系列文章目录...
委外采购订单交期修改导致组件B... 我们公司对供应商的送货交期比较严,一般都要设置分批交货,因此需要经常批量维护计划行的交期,标准功能M...
一口一口吃掉yolov8(1) 1.目标 上一篇讲了怎么训练yolov8, 训练yolov8 但是如果只满足于此&#x...
使用大规模数据注释和深度学习对... 使用大规模数据注释和深度学习对具有人类水平性能的组织图像进行全细胞分割摘要绪论Mesmer2.1Me...
RPA学习-数组处理 RPA学习-数组处理 向数组中添加元素输出数组中某个元素获取指定类型数据查找数组元素下标修改数组元素...
解读 Servlet 源码:G... 解读 Servlet 源码:GenericServlet,Servlet...
GPT-4、百度文心一言摆擂,... 科技云报道原创。 一觉醒来,万众期待的GPT-4来了。OpenAI老板Sam Altm...
系统分析师每日练习错题知识点 计算机网络: RIP协议存在的一个问题就是当网络出现故障的时候,要经过比...
基于YOLOv5的舰船检测与识... 摘要:基于YOLOv5的舰船检测与识别系统用于识别包括渔船、游轮等多种海上船只类型&#...
[simulink] --- ... 1 matlab project的概念 什么是Project(Matlab/Simul...