Qt的插件机制

使用Qt插件和编写Qt插件需要注意:

 

Making an application extensible through plugins involves the following steps:

  1. Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.
  2. Use the Q_DECLARE_INTERFACE() macro to tell Qt's meta-object system about the interface.
  3. Use QPluginLoader in the application to load the plugins.
  4. Use qobject_cast() to test whether a plugin implements a given interface.

Writing a plugin involves these steps:

  1. Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.
  2. Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.
  3. Export the plugin using the Q_EXPORT_PLUGIN2() macro.
  4. Build the plugin using a suitable .pro file

Qmediaserviceproviderplugin.h

 

 

Qstreamerplayerplugin.h

Qsteamerplayerplugin.cpp

Qstreamerplayerplugin继承并实现了两个抽象类。Q_INTERFACES的作用是将所实现的接口通知元类型系统,而Q_EXPORT_PLUGIN2(PluginName, ClassName)指明了插件名和根类名,表示向外部声明本插件的“身份证。

QPluginLoader可以在运行时载入一个插件,它在构造函数中指明一个包含插件的文件(shared library)并用load()导入,其最重要的功能是可以用instance()直接访问这个插件的root component,而不用人工地分析这个文件。而所谓的root component其实就是插件中使用了Q_EXPORT_PLUGIN2的那个类,instance()的返回值就是指向这个类的实例的QObject指针。

一旦插件被导入,它会一直驻留在内存中直到所有的QPluginLoader实例被unload或应用程序终止。你可以调用unload()来试图释放一个插件,但是只有一个插件的所有实例都被调用unload(),插件才会被真正释放。

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值