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 目录结构

 

相关内容

热门资讯

扫房神器2安卓系统,打造洁净家... 你有没有发现,家里的灰尘就像小精灵一样,总是悄悄地在你不注意的时候跳出来?别急,今天我要给你介绍一个...
安卓完整的系统设置,全面掌控手... 亲爱的手机控们,是不是觉得你的安卓手机用久了,功能越来越强大,但设置却越来越复杂?别急,今天就来带你...
电视安卓系统是几代机子,揭秘新... 你有没有想过,家里的电视是不是已经升级到了最新的安卓系统呢?别小看了这个小小的系统升级,它可是能让你...
安卓系统隐私有经常去,系统级防... 你知道吗?在咱们这个数字化时代,手机可是我们生活中不可或缺的好伙伴。但是,你知道吗?这个好伙伴有时候...
安卓10系统断网软件,轻松实现... 你有没有遇到过这种情况?手机突然断网了,明明信号满格,却连不上网,急得你团团转。别急,今天就来给你揭...
安卓可以改什么系统版本,体验全... 你有没有想过,你的安卓手机其实可以像换衣服一样,换一个全新的“系统版本”呢?没错,这就是今天我们要聊...
最好的平板游戏安卓系统,畅享指... 亲爱的游戏迷们,你是否在寻找一款能够让你在安卓平板上畅玩无忧的游戏神器?别急,今天我就要给你揭秘,究...
华为安卓系统卡顿解决,华为安卓... 你是不是也遇到了华为安卓系统卡顿的问题?别急,今天就来给你支几招,让你的华为手机重新焕发活力!一、清...
安卓建议升级鸿蒙系统吗,探讨鸿... 亲爱的安卓用户们,最近是不是被鸿蒙系统的新鲜劲儿给吸引了?是不是在犹豫要不要把你的安卓手机升级成鸿蒙...
安卓如何变苹果系统桌面,桌面系... 你有没有想过,把你的安卓手机变成苹果系统桌面,是不是瞬间高大上了呢?想象那流畅的动画效果,那简洁的界...
windows平板安卓系统升级... 你有没有发现,最近你的Windows平板电脑突然变得有些不一样了?没错,就是那个一直默默陪伴你的小家...
安卓系统扩大运行内存,解锁更大... 你知道吗?在科技飞速发展的今天,手机已经成为了我们生活中不可或缺的好伙伴。而手机中,安卓系统更是以其...
安卓系统怎么改变zenly,探... 你有没有发现,你的安卓手机上的Zenly应用最近好像变得不一样了?没错,安卓系统的大手笔更新,让Ze...
英特尔安卓子系统,引领高效移动... 你有没有想过,手机里的安卓系统竟然也能和电脑上的英特尔处理器完美结合呢?这可不是天方夜谭,而是科技发...
永远会用安卓系统的手机,探索安... 亲爱的手机控们,你是否也有那么一款手机,它陪伴你度过了无数个日夜,成为了你生活中不可或缺的一部分?没...
有哪些安卓手机系统好用,好用系... 你有没有发现,现在手机市场上安卓手机的品牌和型号真是琳琅满目,让人挑花了眼?不过别急,今天我就来给你...
卡片记账安卓系统有吗,便捷财务... 你有没有想过,用手机记账是不是比拿着小本本记录来得方便多了?现在,手机上的应用层出不穷,那么,有没有...
武汉摩尔影城安卓系统APP,便... 你有没有想过,一部手机就能带你走进电影的世界,享受大屏幕带来的震撼?今天,就让我带你详细了解武汉摩尔...
联想刷安卓p系统,畅享智能新体... 你有没有发现,最近联想的安卓P系统刷机热潮可是席卷了整个互联网圈呢!这不,我就迫不及待地来和你聊聊这...
mac从安卓系统改成双系统,双... 你有没有想过,你的Mac电脑从安卓系统改成双系统后,生活会有哪些翻天覆地的变化呢?想象一边是流畅的苹...