gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:41:28 26 4
gpt4 key购买 nike

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

!!!请不要害怕,它只有 2 个接口(interface)和 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(&h) <- 只是插件加载和一些逻辑

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

我的问题是我收到链接错误或运行时未解析的符号(主要是 QObject 的一些想法)或者我的信号无法连接...ProtocolHandler 应该是连接信号/槽的那个......

谁能告诉我如何使这种方法正确? Qt 例子没有涵盖这种想法..

谢谢!

亚当

最佳答案

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

关于c++ - Qt 插件中的 QObject 工厂(非创建者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9070817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com