Qt插件中的QObject工厂(非创建者)

时间:2012-01-30 21:10:49

标签: c++ qt plugins signals qobject

我开始对我的问题非常紧张,我会尽力满足我的需要,希望有人理解。 想象一下,我有一个项目,它生成一个可执行文件和一些插件(运行时加载库)。该可执行文件是某种守护进程/服务,它在开始时寻找合适的插件。所以这些插件应该提供一些抽象的通信层。 我查看了Qt Doc,发现QPluginLoader类应该提供.so的接口加载,但接口不能有信号/槽,必须是纯虚拟的。所以我在思考一些会回归基于QObject的对象...

!!!请不要害怕,它只有2个接口和2个实现:)

我的项目布局和内容:

./守护程序/接口/ 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")

./守护程序/接口/ 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(&amp; h)&lt; - 只是插件加载和一些逻辑

./ 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

./插件/假/ DummyPluginInterface.h

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

./插件/假/ 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)

./插件/假/ DummyProtocolInterface.h

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

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

./插件/假/ DummyProtocolInterface.cpp

#include "DummyProtocolInterface.h"

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

void DummyProtocolInterface::start() {
}

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

./插件/假/ Dummy.pro

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

我的问题是我正在获取链接错误或运行时未解析的符号(主要是来自QObject的一些思考)或者我的信号无法通过连接... ProtocolHandler应该是连接信号/插槽的那个..

有人能告诉我如何使这种方法正确吗? Qt的例子并没有涵盖这样的想法..

谢谢!

亚当

1 个答案:

答案 0 :(得分:2)

所以将ProtocolInterface.h添加到Plugin.pro文件的HEADERS + =部分解决问题:)