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

 

相关内容

热门资讯

安卓系统换成苹果键盘,键盘切换... 你知道吗?最近我在想,要是把安卓系统的手机换成苹果的键盘,那会是怎样的体验呢?想象那是不是就像是在安...
小米操作系统跟安卓系统,深度解... 亲爱的读者们,你是否曾在手机上看到过“小米操作系统”和“安卓系统”这两个词,然后好奇它们之间有什么区...
miui算是安卓系统吗,深度定... 亲爱的读者,你是否曾在手机上看到过“MIUI”这个词,然后好奇地问自己:“这玩意儿是安卓系统吗?”今...
安卓系统开机启动应用,打造个性... 你有没有发现,每次打开安卓手机,那些应用就像小精灵一样,迫不及待地跳出来和你打招呼?没错,这就是安卓...
小米搭载安卓11系统,畅享智能... 你知道吗?最近小米的新机子可是火得一塌糊涂,而且听说它搭载了安卓11系统,这可真是让人眼前一亮呢!想...
安卓2.35系统软件,功能升级... 你知道吗?最近在安卓系统界,有个小家伙引起了不小的关注,它就是安卓2.35系统软件。这可不是什么新玩...
安卓系统设置来电拦截,轻松实现... 手机里总是突然响起那些不期而至的来电,有时候真是让人头疼不已。是不是你也想摆脱这种烦恼,让自己的手机...
专刷安卓手机系统,安卓手机系统... 你有没有想过,你的安卓手机系统是不是已经有点儿“老态龙钟”了呢?别急,别急,今天就来给你揭秘如何让你...
安卓系统照片储存位置,照片存储... 手机里的照片可是我们珍贵的回忆啊!但是,你知道吗?这些照片在安卓系统里藏得可深了呢!今天,就让我带你...
华为鸿蒙系统不如安卓,挑战安卓... 你有没有发现,最近手机圈里又掀起了一股热议?没错,就是华为鸿蒙系统和安卓系统的较量。很多人都在问,华...
安卓系统陌生电话群发,揭秘安卓... 你有没有遇到过这种情况?手机里突然冒出好多陌生的电话号码,而且还是一个接一个地打过来,简直让人摸不着...
ios 系统 安卓系统对比度,... 你有没有发现,手机的世界里,iOS系统和安卓系统就像是一对双胞胎,长得差不多,但细节上却各有各的特色...
安卓只恢复系统应用,重拾系统流... 你有没有遇到过这种情况?手机突然卡顿,或者某个应用突然罢工,你一气之下,直接开启了“恢复出厂设置”大...
安卓系统出现支付漏洞,揭秘潜在... 你知道吗?最近安卓系统可是闹出了不小的风波呢!没错,就是那个我们每天离不开的安卓系统,竟然出现了支付...
苹果换了安卓系统恢复,体验变革... 你有没有遇到过这种情况?手机里的苹果突然变成了安卓系统,而且还是那种让你摸不着头脑的恢复模式。别急,...
安卓怎么卸载系统app,轻松告... 手机里的系统应用越来越多,有时候真的让人眼花缭乱。有些应用虽然看起来很实用,但用起来却发现并不适合自...
安卓系统查看步数,揭秘日常运动... 你有没有发现,每天手机里的小秘密越来越多?今天,咱们就来聊聊安卓系统里那个悄悄记录你每一步的小家伙—...
安卓系统未来会不会,未知。 你有没有想过,那个陪伴我们手机生活的安卓系统,它的未来会怎样呢?想象每天早上醒来,手机屏幕上跳出的信...
安卓系统怎么设置截图,轻松捕捉... 亲爱的手机控们,你是不是也和我一样,有时候想记录下手机屏幕上的精彩瞬间呢?别急,今天就来手把手教你如...
安卓系统下载软件安装,安卓系统... 你有没有发现,手机里的安卓系统就像一个巨大的宝藏库,里面藏着各种各样的软件,让人眼花缭乱。今天,就让...