当前位置:网站首页>在Qt中如何编写插件,加载插件和卸载插件
在Qt中如何编写插件,加载插件和卸载插件
2022-07-29 01:17:00 【天天进步2015】
Qt提供了一个类QPluginLoader来加载静态库和动态库,在Qt中,Qt把动态库和静态库都看成是一个插件,使用QPluginLoader来加载和卸载这些库。由于在开发项目的过程中,要开发一套插件系统,就使用了Qt的这套类库。
一 编写插件
编写一个Qt的插件需要以下步骤
1.声明一个插件类,
2.定义一个类,实现这个插件类定义的接口,定义的这个类必须从QObject集成下来。
3.使用Q_INTERFACESQ_INTERFACES()告知Qt的元系统这个接口的存在
3.使用宏Q_EXPORT_PLUGIN2()来导出插件的接口
4.编写插件类的.pro文件
下面是一个插件的例子
class FilterInterface
{
public:
virtual ~FilterInterface() {}
virtual QStringList filters() const = 0;
virtual QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent) = 0;
};
注意:接口类必须是一个纯虚函数
Q_DECLARE_INTERFACE(FilterInterface,"com.trolltech.PlugAndPaint.BrushInterface/1.0")
为了使得程序在运行时刻可以查询一个插件是否实现了给定的接口,必须使用宏Q_DECLARE_INTERFACE()。他的第一个参数是这个接口的名字,第二个参数是一个确定接口的字符串。方便起见,使用了JAVA包的命名方法。如果后来我们改变接口,必须使用一个新的字符串来确认接口。否则,应用程序可能崩溃,所以包括一个版本号是一个不错的方法。
#include <QObject>
#include <QStringList>
#include <QImage>
#include <plugandpaint/interfaces.h>
class ExtraFiltersPlugin : public QObject, public FilterInterface
{
Q_OBJECT
Q_INTERFACES(FilterInterface)
public:
QStringList filters() const;
QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent);
};
实现这个接口类。实现接口的这个类必须从QObject派生下来,并且必须实现接口类里定义的虚函数。
Q_EXPORT_PLUGIN2(PlugExtraFilters, ExtraFiltersPlugin )
由于应用函数使用main()函数作为入口点,插件必须使用宏Q_EXPORT_PLUGIN2()来指定那个类提供插件。
这一行代码可以出现在实现接口类的.cpp文件的任何一行。其中第一个参数为插件的名称,第二个参数为插件类
二 使用插件
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin) {
populateMenus(plugin);
pluginFileNames += fileName;
}
QPluginLoader类提供了一个函数loader来加载插件的实例,如果动态链接库不是一个插件,或者编译的Qt的版本库不对,返回的
QObject对象的指针为空。】
使用unload函数来卸载插件。
三 .pro文件的编写
TEMPLATE = lib CONFIG += plugin HEADERS = extrafiltersplugin.h SOURCES = extrafiltersplugin.cpp TARGET = pnp_extrafilters DESTDIR = ../../plugandpaint/plugins
TEMPLATE的类型为lib.CONFIG的类型为plugin
边栏推荐
- Lua third-party byte stream serialization and deserialization module --lpack
- LeetCode 112:路径总和
- 数学建模——仓内拣货优化问题
- Lxml web page capture the most complete strategy
- Using local cache + global cache to realize user rights management of small systems
- 使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
- 【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】
- Mathematical modeling - location of police stations
- Golang startup error [resolved]
- leetcode/乘积小于K 的连续子数组的个数
猜你喜欢

【流放之路-第五章】

Mathematical modeling -- Optimization of picking in warehouse

druid. io index_ Realtime real-time query

【流放之路-第八章】

Planning mathematics final exam simulation II

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
![[WesternCTF2018]shrine](/img/c1/c099f8930902197590052630281258.png)
[WesternCTF2018]shrine

Wonderful use of data analysis

Process -- user address space and kernel address space

What are the common cyber threats faced by manufacturers and how do they protect themselves
随机推荐
How to prevent all kinds of affiliated fraud?
Using local cache + global cache to realize user rights management of small systems
What are the common cyber threats faced by manufacturers and how do they protect themselves
Mathematical modeling -- Optimization of picking in warehouse
[云原生]微服务架构是什么
【流放之路-第四章】
JS dom2 and dom3
[MySQL] SQL aliases the table
[the road of Exile - Chapter 6]
Golang run times undefined error [resolved]
知道创宇上榜CCSIP 2022全景图多个领域
iVX低代码平台系列详解 -- 概述篇(二)
Talk about possible problems when using transactions (@transactional)
druid. IO custom real-time task scheduling policy
[网鼎杯 2020 朱雀组]Nmap
Stonedb invites you to participate in the open source community monthly meeting!
Day01 job
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
The scientific research environment has a great impact on people
抓包工具Charles使用