如何在Qt中模拟消息总线?

发布于 2024-09-29 12:59:10 字数 263 浏览 6 评论 0原文

我需要实现一个简单的消息总线:

  • 只有一个进程,因此不需要 D-Bus。
  • 发布/订阅类型事件(甚至可能是 QObjects)

我正在考虑使用 QSignalMapper 来标记“命名事件”,然后从插槽重新发出或将发布者信号连接到订阅者信号...

有什么建议吗?或者我应该选择相对简单的设计模式?

PS:Windows 上 D-Bus 的 AFAICS 您需要安装“第 3 方”软件才能与 Qt 配合使用。

I need to implement a simple message bus:

  • One process only thus no need do D-Bus.
  • Publish/subscribe to typed events (Could even be QObjects)

I was thinking of using QSignalMapper to tag the "named events", then re-emitting from a slot or connecting the publishers signal to the subscriber's signal...

Any suggestions thoughts? Or should I go for the relatively simple design pattern?

PS: AFAICS for D-Bus on windows you need to install "3rd party" software to get it going with Qt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-10-06 12:59:10

为什么不只使用一个专用的 QObject 子类作为消息总线呢?在那里,您定义可能通过消息总线交换的所有信号,并提供发出这些信号的相应通知方法。现在,每个想要接收“消息”的组件都可以连接到感兴趣的信号。

如果您想要更通用的方法,请使用与以前相同的方法。然而,(单例)QObject 子类现在只有一个“message(QByteArray)”信号和一个发出该信号的“sendMessage(QByteArray)”公共方法。您可能还想将发送消息方法声明为槽,以防万一您想将另一个信号连接到发送方法。

我自己使用这些方法并且效果非常好。即使不同的线程也可以使用这种机制相互通信,不会出现任何问题。如果您使用 QByteArray 方法,您将得到类似于 DBus 的东西。您可以序列化和反序列化消息,并自动确保所有消息接收者都获得自己的消息副本,并享受并行计算带来的所有好处。

Why don't you just use one dedicated QObject subclass as a message bus? There you define all signals hat might be exchanged over the message bus and you provide corresponding notify methods that emit these signals. Now every component that wants to receive "messages" can connect to the signals of interest.

If you want a more generic method use the same approach as before. However, the (singleton) QObject subclass now only has a "message(QByteArray)" signal and a "sendMessage(QByteArray)" public method which emits this signal. You might want to declare the send message method as being a slot, too, just in case you want to connect another signal to the send method.

I use these approaches myself and they work perfecty fine. Even different threads can communicate with each other using this mechanism without any problems. If you use the QByteArray approach you will get something similar to DBus. You serialize and deserialize your messages and automatically make sure all message receivers get their own copy of messages with all the benefits you get if you do parallel calculations.

天煞孤星 2024-10-06 12:59:10

你可以试试这个。这正是您想要的。它重量轻且易于使用。
https://github.com/lheric/libgitlevtbus

#include "gitlmodual.h"
#include <QDebug>

int main(int argc, char *argv[])
{
    GitlModual cModual;

    /// subscribe to an event by name ("I am a test event")
    cModual.subscribeToEvtByName("I am a test event",
    [](GitlEvent& rcEvt)->bool                          ///< lambda function as a callback
    {
        qDebug() << "Hello GitlEvtBus!";
        return true;
    }
    );

    GitlEvent cEvent("I am a test event");              ///< create an event
    cEvent.dispatch();                                  ///< dispatch

    /// output: "Hello GitlEvtBus!"

    return 0;
}

You can try this. It is excatly what you want. It is light-weighted and easy to use.
https://github.com/lheric/libgitlevtbus

#include "gitlmodual.h"
#include <QDebug>

int main(int argc, char *argv[])
{
    GitlModual cModual;

    /// subscribe to an event by name ("I am a test event")
    cModual.subscribeToEvtByName("I am a test event",
    [](GitlEvent& rcEvt)->bool                          ///< lambda function as a callback
    {
        qDebug() << "Hello GitlEvtBus!";
        return true;
    }
    );

    GitlEvent cEvent("I am a test event");              ///< create an event
    cEvent.dispatch();                                  ///< dispatch

    /// output: "Hello GitlEvtBus!"

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文