相信积累的力量

qt 插件机制

http://blog.csdn.net/shuyi18/article/details/5983008

使用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

 

 

[c-sharp] view plaincopy
 
  1. struct Q_MULTIMEDIA_EXPORT QMediaServiceProviderFactoryInterface : public QFactoryInterface    
  2. {    
  3.     ...    
  4. };    
  5. #define QMediaServiceProviderFactoryInterface_iid /    
  6.     "com.nokia.Qt.QMediaServiceProviderFactoryInterface/1.0"    
  7. Q_DECLARE_INTERFACE(QMediaServiceProviderFactoryInterface, QMediaServiceProviderFactoryInterface_iid)    
  8. struct Q_MULTIMEDIA_EXPORT QMediaServiceSupportedDevicesInterface  
  9. {  
  10.     ...  
  11. };  
  12. #define QMediaServiceSupportedDevicesInterface_iid /  
  13.     "com.nokia.Qt.QMediaServiceSupportedDevicesInterface/1.0"  
  14. Q_DECLARE_INTERFACE(QMediaServiceSupportedDevicesInterface, QMediaServiceSupportedDevicesInterface_iid)  
  15.     
  16. class Q_MULTIMEDIA_EXPORT QMediaServiceProviderPlugin : public QObject, public QMediaServiceProviderFactoryInterface    
  17. {    
  18.     Q_OBJECT    
  19.     Q_INTERFACES(QMediaServiceProviderFactoryInterface:QFactoryInterface)    
  20. public:    
  21.     ...    
  22. };   

 

 

Qstreamerplayerplugin.h

 

[cpp] view plaincopy
 
  1. class QGstreamerServicePlugin : public QMediaServiceProviderPlugin, public QMediaServiceSupportedDevicesInterface  
  2. {  
  3.     Q_OBJECT  
  4.     Q_INTERFACES(QMediaServiceSupportedDevicesInterface)  
  5. public:  
  6.     ...  
  7. private:  
  8.     ...  
  9. };  

 

Qsteamerplayerplugin.cpp

 

[cpp] view plaincopy
 
  1. #include "qstreamerplayerplugin.h"  
  2. //Implementation  
  3. ...  
  4. Q_EXPORT_PLUGIN2(qtmedia_gstengine, QGstreamerServicePlugin);  

 

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(),插件才会被真正释放。

posted @ 2013-10-02 16:10  ThreeF  阅读(631)  评论(0编辑  收藏  举报

相信积累的力量