当前位置:网站首页>How to write, load and unload plug-ins in QT
How to write, load and unload plug-ins in QT
2022-07-29 02:09:00 【Progress every day 2015】
Qt Provides a class QPluginLoader To load static and dynamic libraries , stay Qt in ,Qt Consider both dynamic library and static library as a plug-in , Use QPluginLoader To load and unload these libraries . Because in the process of developing the project , To develop a plug-in system , I used Qt This class library of .
One Writing a plug-in
Write a Qt The plug-in of requires the following steps
1. Declare a plug-in class ,
2. Define a class , Implement the interface defined by this plug-in class , This class must be defined from QObject Integrated .
3. Use Q_INTERFACESQ_INTERFACES() inform Qt The existence of the interface of the meta system
3. Use macros Q_EXPORT_PLUGIN2() To export the interface of the plug-in
4. Write a plug-in class .pro file
Here is an example of a plug-in
class FilterInterface
{
public:
virtual ~FilterInterface() {}
virtual QStringList filters() const = 0;
virtual QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent) = 0;
};
Be careful : The interface class must be a pure virtual function
Q_DECLARE_INTERFACE(FilterInterface,"com.trolltech.PlugAndPaint.BrushInterface/1.0")
In order to enable the program to query whether a plug-in implements a given interface at runtime , You must use macros Q_DECLARE_INTERFACE(). Its first parameter is the name of the interface , The second parameter is a string that determines the interface . Convenience , Used JAVA Package naming method . If we change the interface later , You must use a new string to confirm the interface . otherwise , The application may crash , So including a version number is a good way .
#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);
};
Implement this interface class . The class that implements the interface must start from QObject Derived , And you must implement the virtual function defined in the interface class .
Q_EXPORT_PLUGIN2(PlugExtraFilters, ExtraFiltersPlugin )
Because the application function uses main() Function as the entry point , Plug ins must use macros Q_EXPORT_PLUGIN2() To specify which class provides plug-ins .
This line of code can appear in the .cpp Any line of the file . The first parameter is the name of the plug-in , The second parameter is the plug-in class
Two The use of plug-in
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin) {
populateMenus(plugin);
pluginFileNames += fileName;
}
QPluginLoader Class provides a function loader To load an instance of the plug-in , If the DLL is not a plug-in , Or compiled Qt Wrong version Library , Back to
QObject The pointer to the object is null .】
Use unload Function to uninstall the plug-in .
3、 ... and .pro Compilation of documents
TEMPLATE = lib CONFIG += plugin HEADERS = extrafiltersplugin.h SOURCES = extrafiltersplugin.cpp TARGET = pnp_extrafilters DESTDIR = ../../plugandpaint/plugins
TEMPLATE The type of lib.CONFIG The type of plugin
边栏推荐
- [the road of Exile - Chapter 4]
- Mathematical modeling -- heat conduction of subgrade on Permafrost
- Verilog procedure assignment statements: blocking & non blocking
- LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
- Overview of Qualcomm 5g intelligent platform
- leetcode/和大于等于target的连续最短子数组
- (arxiv-2018) reexamine the time modeling of person Reid based on video
- druid. io kill -9 index_ Realtime traceability task
- Leetcode 112: path sum
- 什么是作用域和作用域链
猜你喜欢

ASCII code table

Comprehensive use method of C treeview control

给LaTeX公式添加优美的注解;日更『数据科学』面试题集锦;大学生『计算机』自学指南;个人防火墙;前沿资料/论文 | ShowMeAI资讯日报

【流放之路-第八章】
[electronic components] zener diode

Yocto project download and compilation

Mathematical modeling -- cold proof simulation of low temperature protective clothing with phase change materials

Day01 job

物联网开发--MQTT消息服务器EMQX

Planning mathematics final exam simulation II
随机推荐
Talk about possible problems when using transactions (@transactional)
Mathematical modeling -- heat conduction of subgrade on Permafrost
leetcode/乘积小于K 的连续子数组的个数
Control the pop-up window and no pop-up window of the input box
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
Mathematical modeling -- the laying of water pipes
What is browser fingerprint recognition
Have you ever encountered the situation that the IP is blocked when crawling web pages?
Thirty years of MPEG audio coding
Random talk on distributed development
Use POI to export excel file, image URL to export file, image and excel file to export compressed package
Web crawler API Quick Start Guide
Monadic linear function perceptron: Rosenblatt perceptron
Explanation of yocto project directory structure
Number of consecutive subarrays with leetcode/ and K
Stonedb invites you to participate in the open source community monthly meeting!
FPGA实现10M多功能信号发生器
12.< tag-动态规划和子序列, 子数组>lt.72. 编辑距离
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform