当前位置:网站首页>在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
边栏推荐
- [the road of Exile - Chapter 7]
- Leetcode/ and continuous shortest subarray greater than or equal to target
- Wonderful use of data analysis
- 秘术冬潮烙技能搭配
- 【流放之路-第七章】
- Use POI to export excel file, image URL to export file, image and excel file to export compressed package
- Promise解决异步
- Large scale web crawling of e-commerce websites (Ultimate Guide)
- Explanation of yocto project directory structure
- 【流放之路-第五章】
猜你喜欢
![[WesternCTF2018]shrine](/img/c1/c099f8930902197590052630281258.png)
[WesternCTF2018]shrine

Mysql存储json格式数据

【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】

【流放之路-第三章】

LeetCode 练习——剑指 Offer 45. 把数组排成最小的数

数学建模——红酒品质分类

Yocto project download and compilation

知道创宇上榜CCSIP 2022全景图多个领域

StoneDB 邀请您参与开源社区月会!
![[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform](/img/e7/1d06eba0e50eeb91d2d5da7524f4af.png)
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
随机推荐
What are the common cyber threats faced by manufacturers and how do they protect themselves
druid. io kill -9 index_ Realtime traceability task
What is a proxy server? [2022 guide]
Comprehensive analysis of news capture doorway
ASCII code table
How to prevent all kinds of affiliated fraud?
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
Planning mathematics final exam simulation II
正则过滤数据学习笔记(①)
StoneDB 邀请您参与开源社区月会!
Lua log implementation -- print table
Thirty years of MPEG audio coding
(arxiv-2018) reexamine the time modeling of person Reid based on video
[golang] network connection net.dial
Data platform data access practice
(CVPR-2019)选择性的内核网络
Leetcode 113: path sum II
【流放之路-第五章】
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
[golang] synchronization lock mutex