Qt插件之间的管道数据 [英] Qt piping data between plugins

查看:363
本文介绍了Qt插件之间的管道数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于插件的应用,遵循回声插件示例

I'm developing an app based on plugins, following the Echo Plugin Example.

到目前为止很好。现在我在一个点,我想在插件之间管道数据。我有一个插件使用 QTcpSocket 建立网络连接,我有另一个插件,其中我想显示在 QTextEdit 。如何在插件之间管道数据?

So far so good. Now I'm at a point where I would like to pipe data between plugins. I have one plugin which establishes a network connection using QTcpSocket and I have another plugin in which I would like to display the data in a QTextEdit. How do I pipe the data between plugins?

如果两个简单的对象,我将做简单的信号槽连接,但由于插件的接口不是从QObject派生的不可能有信号槽。或者,至少我不知道如何。

If both where simple objects I will do simple signal slot connection, but since the interface of plugins doesn't derive from QObject it's not possible to have signal slots. Or, at least I don't know how.

推荐答案

实际上,信号槽是可能的。所有你需要做的是将你的插件转换为QObject。示例:

Actually, signal-slots is possible. All you have to do is to cast your plugin to a QObject. Example:

您的插件界面:

class MyPluginInterface1
{
public:
    virtual ~MyPluginInterface1() {}

signals:
    virtual void mySignal() = 0;
}

class MyPluginInterface2
{
public:
    virtual ~MyPluginInterface2() {}

public slots:
    virtual void mySlot() = 0;
}

Q_DECLARE_INTERFACE(MyPluginInterface1, ...)
Q_DECLARE_INTERFACE(MyPluginInterface2, ...)

现在,在实现中,您可以:

Now, in your implementation, you do:

class MyPlugin1 : public QObject, public MyPluginInterface1
{
    Q_OBJECT
    Q_PLUGIN_METADATA(...)
    Q_INTERFACES(MyPluginInterface1)

signals:
    void mySignal() Q_DECL_FINAL;//make it final because signals cannot be overridden
}

class MyPlugin2 : public QObject, public MyPluginInterface2
{
    Q_OBJECT
    Q_PLUGIN_METADATA(...)
    Q_INTERFACES(MyPluginInterface2)

public slots:
    void mySlot() Q_DECL_OVERRIDE;
}

最后,当进行连接时

//...
MyPluginInterface1 *p1 = this->plugin1;//Just an example
MyPluginInterface2 *p1 = this->plugin2;

//since you know both inherit QObject, you can do:
QObject::connect(dynamic_cast<QObject*>(p1), SIGNAL(mySignal()),
                 dynamic_cast<QObject*>(p2), SLOT(mySlot()));

//...

请注意,Qt不会提供自动

Please note that Qt will not provide auto-completion for those signals/slots and you have to connect them using this (old) syntax.

提示:如果您希望在接口中有信号,请理解这些信号/插槽的信号/插槽必须使用此(旧)语法连接。信号不是虚拟的,它们不能被覆盖。但是,要将信号添加到您的接口,它必须是纯虚拟的。为避免错误,请始终使用 Q_DECL_FINAL 宏实现信号 - 这将阻止您覆盖它们。

Hint: If you want to have signals in your interfaces, please understand that signals are not virtual, they cannot be overriden. However, to add the signal to your interface, it has to be pure virtual. To avoid errors, always implement the signals using the Q_DECL_FINAL macro - this prevents you from overriden them.

这篇关于Qt插件之间的管道数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆