1. 程式人生 > >Qt5的外掛機制(7)--外掛開發示例程式碼(Lower-level API)

Qt5的外掛機制(7)--外掛開發示例程式碼(Lower-level API)

外掛程式碼

介面類標頭檔案 MyPluginInterface.h

  1. #ifndef INTERFACES_H
  2. #define INTERFACES_H
  3. #include <QtPlugin>
  4. #define QtPluginDemo_iid "org.qt-project.Qt.PluginDemo"
  5. class MyPluginInterface  
  6. {  
  7. public:  
  8.     virtualint add(int,int) = 0 ;  // 正確寫法
  9.     // virtual int add(int,int);    // 不太妥的寫法(雖然這也是正確的虛擬函式宣告語句)
  10.         // 注:介面類中的虛擬函式都應有實體,或設定為0(推薦),這樣
  11.         // C++的編譯器才能為介面類生成虛擬函式表。否則,如果沒有實現
  12.         // 介面類的虛擬函式也沒有將其設為0,程式在在連結時可能會失敗,
  13.         // 或者連結成功但最後生成的庫無法載入(庫中有未定義的符號)
  14. };  
  15. Q_DECLARE_INTERFACE ( MyPluginInterface, QtPluginDemo_iid ) ;  
  16. #endif

外掛標頭檔案 MyPlugin.h

  1. #ifndef MYPLUGIN__H
  2. #define MYPLUGIN__H
  3. #include <QObject>
  4. #include <QDebug>
  5. #include "MyPluginInterface.h"
  6. class MyPlugin : public QObject, public MyPluginInterface  
  7. {  
  8.     Q_OBJECT  
  9.     Q_PLUGIN_METADATA ( IID QtPluginDemo_iid FILE"MyPlugin.json")  
  10.     Q_INTERFACES(MyPluginInterface)  
  11. public:  
  12.     int add(int,int);  
  13. };  
  14. #endif

外掛原始檔 MyPlugin.cpp
  1. #include "MyPlugin.h"
  2. int MyPlugin::add(int a , int b)  
  3. {  
  4.     return a+b ;  
  5. }  
  6. #include "moc_MyPlugin.cpp"

JSON檔案 MyPlugin.json,本示例中該檔案是空的。

工程檔案 MyPlugin.pro

  1. TEMPLATE      = lib  
  2. CONFIG       += plugin console  
  3. QT           += core  
  4. HEADERS       = MyPlugin.h MyPluginInterface.h  
  5. SOURCES       = MyPlugin.cpp  
  6. OTHER_FILES   = MyPlugin.json  
  7. TARGET        = MyPlugin  
  8. DESTDIR       = ./  
  9. INCLUDEPATH  += ./  
  10. # install
  11. target.path = ./install  
  12. INSTALLS += target  


應用程式程式碼

介面類標頭檔案 MyPluginInterface.h,該檔案與外掛程式碼中的一致。

主程式檔案 main.cpp

  1. #include "MyPluginInterface.h"
  2. #include <QtPlugin>
  3. #include <QApplication>
  4. #include <QWidget>
  5. #include <QPluginLoader>
  6. #include <QString> 
  7. #include <QtDebug>
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication app(argc, argv);  
  11.     QWidget w;  
  12.     QObject* object ;  
  13.     w.show();  
  14.     app.addLibraryPath(QString("../MyPlugin/install")); // 新增庫路徑
  15.     // 載入外掛,取得例項
  16.     QPluginLoader l(QString("MyPlugin")) ;  
  17.     //QPluginLoader l(QString("libMyPlugin.so")) ;
  18.     if ( (object=l.instance()) != NULL )  
  19.     {  
  20.         qDebug("plugin loaded .");  
  21.         // 使用外掛
  22.         int a = 14 , b = 23 ;  
  23.         MyPluginInterface* plugin = qobject_cast<MyPluginInterface*>(object) ;  
  24.         if (plugin)  
  25.             qDebug("%d + %d = %d",a,b,plugin->add(a,b));  
  26.     }  
  27.     else
  28.     {  
  29.         qDebug("failed to load plugin !! ");  
  30.         QString errorStr = l.errorString();  
  31.         qDebug()<<errorStr;  
  32.     }  
  33.     return app.exec();  
  34. }  

MyApp.pro
  1. ######################################################################
  2. # Automatically generated by qmake (3.0) ?? 11? 19 02:26:33 2014
  3. ######################################################################
  4. TEMPLATE = app  
  5. QT += gui core widgets  
  6. CONFIG += console  
  7. TARGET = MyApp  
  8. INCLUDEPATH += .  
  9. # Input
  10. HEADERS += MyPluginInterface.h  
  11. SOURCES += main.cpp  


from: http://blog.csdn.net/newthinker_wei/article/details/41338447