Qt插件机制的学习
来源:互联网 发布:sql语句中降序排列 编辑:程序博客网 时间:2024/05/29 08:04
Mesh Deformer的架构若想参照OpenFlipper来实现,首先需要了解Qt plugin system的结构以及如何编写qt plugins.
以下摘抄了Qt文档中的部分内容(只与自己想要实现的功能相关的内容)
The Lower-Level API: Extending Qt Applications
Not only Qt itself but also Qt application can be extended through plugins. This requires the application to detect and load plugins using QPluginLoader. In that context, plugins may provide arbitrary functionality and are not limited to database drivers, image formats, text codecs, styles, and the other types of plugin that extend Qt's functionality.
Making an application extensible through plugins involves the following steps:
- Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.
- Use the Q_DECLARE_INTERFACE() macro to tell Qt's meta-object system about the interface.
- Use QPluginLoader in the application to load the plugins.
- Use qobject_cast() to test whether a plugin implements a given interface.
Writing a plugin involves these steps:
- Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.
- Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.
- Export the plugin using the Q_EXPORT_PLUGIN2() macro.
- Build the plugin using a suitable .pro file
Qt的插件机制可以用来动态地添加功能,而这个扩展通过接口来实现。具体的包括两个方面,一个是插件本身需要对外声称自己提供哪些功能,另一方面是在调用插件的部分(主程序)需要知道如何导入插件。
我参照Qt文档写了如下的测试程序:
(首先,是接口的代码,利用Q_DECLARE_INTERFACe来声明提供接口的类型)
插件,实现接口特定的功能
需要注意的是,插件向外声明提供接口的宏Q_EXPORT_PLUGIN2需放在CPP文件中,不然链接的时候会出现多个link的情况。
而且,这里面的第一个参数为实例的名称,就与QMAKE或CMAKE的产生的目标的名称一致。即此处的test_plugin应与下面CMAKELISTS中的add_library中生成的目标一致。
在这个目录下相对应的CMakeLists.txt如下:
插件实际上看作是shared library的形式。
另外,在主程序那一方,通过导入(windows下为dll,linux下为so,mac下为.dylib)的形式来为主程序添加新的功能
主程序如下:
相对应的cmake文件如下:
本来Qt的plugins有特定的存放目录(plugins),但这里纯粹为测试能否正确载入,所以在实现文件中hard code了。
测试:
在该目录下用以下命令测试:
mkdir build
cd build
cmake ..
make
./test
运行后会打印一个“导入成功”的提示。
源代码下载地址: http://download.csdn.net/source/2314773
- Qt插件机制的学习
- Qt插件机制的学习
- Qt插件机制的学习
- Qt的插件机制
- Qt的插件机制
- Qt的插件机制
- Qt的插件机制
- Qt的插件机制(转)
- QT插件机制
- QT插件机制
- Qt 插件机制以及插件中几个重要的宏
- Qt5的插件机制(1)--Qt 框架中的插件加载机制概述
- Qt插件使用的学习笔记
- Qt 插件学习
- Qt 插件学习(一)
- Qt 插件学习(二)
- QT插件学习
- Qt插件学习
- CentOS 5.4 x86_64上安装simspark笔记
- 2010年SQLite3学习笔记之四(性能优化篇)
- Oralce常识
- c++ 静态数据成员和静态成员函数
- mapxtreme6.8 图片显示黑框
- Qt插件机制的学习
- bzr
- flv视频无法播放,显示video not found
- Getting Contiguous Space Currently in the Shared Pool(Oracle10g Tuning Technology)
- __asm__ __volatile__ ("" : : : "memory")
- 怎样当您按下回车键时使您的光标移到下一个文本输入框
- 数据库设计流程
- 今天遇到的很恼火的问题identifier of an instance of jp.co.gecj.ito.model.TIipitomanage was altered from 2 to 1
- 最新服务器测试方法详细解析