2012-01-30 46 views
0

我開始很緊張與我的問題,我會盡量desribe我的需要,希望有人理解。 想象一下,我有生成一個可執行文件和一些插件(運行時加載的庫)的項目。該可執行文件是某種守護程序/服務,它在一開始就尋找合適的插件。所以這些插件應該提供一些抽象的通信層。 我看過Qt Doc,發現QPluginLoader類應該提供來自.so的接口加載,但接口不能有信號/插槽,必須是純虛擬的。所以我正在考慮一些將返回基於QObject的對象的思考......QObject的工廠在Qt的插件(非創始人)

!!!請不要感到害怕,它只是2接口和2個實現:)

我的項目佈局和內容:

./Daemon/Interfaces/PluginInterface.h

#include <QObject> 
#include "PluginInterface.h" 
class PluginInterface { 
public: 
    virtual ~PluginInterface() = 0; 
    virtual ProtocolInterface* getInterface() = 0; 
    virtual int getPluginId() const = 0; 
}; 

Q_DECLARE_INTERFACE(PluginInterface, "com.porta.protocol.PluginInterface/1.0") 

./Daemon/Interfaces/ ProtocolInterface.h

#include <QObject> 
#include "turnstile.h" 
class ProtocolInterface : public QObject { 
    Q_OBJECT 
public: 
    ProtocolInterface(QObject *parent = 0) : QObject(parent) {} 
    virtual QWidget* getConfigureGUI() = 0; 
    virtual void init() = 0; 
    virtual void start() = 0; 
signals: 
    void someSignal(); 
}; 

./Daemon/ProtocolHander.cpp(& 1H)< - 只是插件加載和一些邏輯

./Daemon.pro

QT  += core gui 
TARGET = porta_daemon 
CONFIG += console 
CONFIG -= app_bundle 
TEMPLATE = app 

SOURCES += main.cpp \ 
protocolhandler.cpp 

HEADERS += protocolhandler.h \ 
Interfaces/protocolinterface.h \ 
Interfaces/protocolloader.h \ 
    Interfaces/turnstile.h 

./Plugins/Dummy/DummyPluginInterface.h

#include "protocolloader.h" 
#include <QObject> 
class DummyPluginInterface : public QObject, PluginInterface { 
    Q_OBJECT 
    Q_INTERFACES(PluginInterface) 
public: 
    ProtocolInterface* getInterface(); 
    int getPluginId() const; 
}; 

./Plugins/Dummy/DummyPluginInterface.cpp

#include "DummyPluginInterface.h" 
#include "DummyProtocolInterface.h" 

ProtocolInterface *DummyPluginInterface::getInterface() { 
    return new DummyProtocolInterface(); 
} 

int DummyPluginInterface::getPluginId() const { 
    return 1; 
} 

Q_EXPORT_PLUGIN2(dummyplugin, DummyPluginInterface) 

./ Plugins/Dummy/DummyProtocolInterface.h

#include "protocolinterface.h" 
#include <QObject> 

class DummyProtocolInterface : public ProtocolInterface { 
public: 
    void init(); 
    QWidget* getConfigureGUI(); 
    void start(); 
    int getPluginId() { return 1; } 
}; 

./Plugins/Dummy/DummyProtocolInterface.cpp

#include "DummyProtocolInterface.h" 

QWidget* DummyProtocolInterface::getConfigureGUI() { 
    return 0; 
} 

void DummyProtocolInterface::start() { 
} 

void DummyProtocolInterface::init() { 
    emit someSignal(); /// !!! this is important for me 
} 

./Plugins/Dummy/Dummy.pro

TEMPLATE  = lib 
CONFIG   += plugin 
QT    += network 
INCLUDEPATH += ../../Daemon/Interfaces/ 
HEADERS  += **** 
SOURCES  += **** 
TARGET   = ***** 
DESTDIR   = ***** 

我的問題的探析是,我收到鏈接錯誤或運行時尚未解決的符號(大多somethink自QObject)或我的信號不能由連接... ProtocolHandler應了一句,誰連接信號/插槽..

誰能告訴我如何使這種做法對嗎? Qt的例子並沒有涵蓋這樣的想法..

謝謝!

亞當

+0

什麼鏈接錯誤? – 2012-01-31 06:40:40

+0

例如,這一個...「libdummyplugin.so:未定義的符號:_ZN17ProtocolInterface16staticMetaObjectE)」 ...但我的同事建議我,放置ProtocolInterface.h成頭標Plugin.pro文件+ =節...我回家後,從工作,我會嘗試 – 2012-01-31 07:42:31

+0

是的,他是正確的,它表明你缺少來自ProtocolInterface類的元數據。 – 2012-01-31 07:45:42

回答

2

因此增加ProtocolInterface.h成頭標+ = Plugin.pro文件的部分解決了這個問題:)