QT Plugin 插件开发

news/2025/2/11 18:50:22/

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

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

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

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 <QMainWindow>#include <QDir>
#include <QDebug>
#include <QMessageBox>
#include <QPluginLoader>#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<DeclareInterface *>(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 <QWidget>//定义接口
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 <QObject>
#include <QtPlugin>
#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 <QWidget>//定义接口
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 目录结构

 


http://www.ppmy.cn/news/32584.html

相关文章

全连接神经网络

目录 1.全连接神经网络简介 2.MLP分类模型 2.1 数据准备与探索 2.2 搭建网络并可视化 2.3 使用未预处理的数据训练模型 2.4 使用预处理后的数据进行模型训练 3. MLP回归模型 3.1 数据准备 3.2 搭建回归预测网络 1.全连接神经网络简介 全连接神经网络(Multi-Layer Percep…

【MySQL】聚合查询

目录 1、前言 2、插入查询结果 3、聚合查询 3.1 聚合函数 3.1.1 count 3.1.2 sum 3.1.3 avg 3.1.4 max 和 min 4、GROUP BY 子句 5、HAVING 关键字 1、前言 前面的内容已经把基础的增删改查介绍的差不多了&#xff0c;也介绍了表的相关约束&#xff0c; 从本期开始…

关于SQL优化的几点说明

1. ORACLE DBA是如何进行SQL优化的 作为一个Oracle数据库管理员(DBA),SQL优化是他们的日常工作之一,主要目标是优化查询性能,减少查询时间,并提高数据库的整体性能。 以下是Oracle DBA如何进行SQL优化的一般流程: 监控和诊断:首先,DBA需要通过Oracle的监控工具来监控数据…

改进YOLO系列 | CVPR2023最新 PConv | 提供 YOLOv5 / YOLOv7 / YOLOv7-tiny 模型 YAML 文件

DWConv是Conv的一种流行变体,已被广泛用作许多神经网络的关键构建块。对于输入 I ∈ R c h w I \in R^{c \times h \times w} I∈

windows 系统下 workerman 在同一个运行窗口中开启多个 websocket 服务

✨ 目录&#x1f388; 开启多个 ws 服务失败&#x1f388; 开启服务失败解决办法&#x1f388; 同一个窗口中运行&#x1f388; 开启多个 ws 服务失败 正常情况下&#xff0c;如果你想开启多个 websocket 服务的话只要在一个文件中&#xff0c;输入 new Worker 两次&#xff0…

链表相关oj题

1.Leetcode203 移除链表元素 解题思路&#xff1a;从头节点开始进行元素删除&#xff0c;每删除一个元素&#xff0c;需要重新链接节点 struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode*dummyheadmalloc(sizeof(struct ListNode));dummyhea…

操作系统之进程的初步认识(1)

进程1. 进程的相关概念1.1 进程的定义1.2 进程的概念(1)1.3 进程的概念(2)2. 进程和程序的区别3. 进程管理:3.1 进程的结构体有哪些属性(1) Pid(操作系统里指进程识别号)(2) 内存指针(3) 文件描述符表4. 进程调度:(1) 并行(2) 并发5. 进程调度需要的属性(1) 进程状态(2) 进程优…

SpringBoot-核心技术篇

技术掌握导图 六个大标题↓ 配置文件web开发数据访问单元测试指标指控原理解析 配置文件 1.文件类型 1.1、properties 同以前的properties用法 1.2、yaml 1.2.1、简介 YAML是 “YAML Aint Markup Language”&#xff08;YAML不是一种标记语言&#xff09;的递归缩写。在…