简体   繁体   English

Qt中的信号共享架构

[英]Signal sharing architecture in Qt

Using MVC, I have several view classes, all of which need to write to an event log. 使用MVC,我有几个视图类,所有这些都需要写入事件日志。 The event log contains a slot called addEntry which writes the data to the log. 事件日志包含一个名为addEntry的插槽,该插槽将数据写入日志。 I'm struggling with how to implement the signals. 我正在努力实现信号。 I don't want to have to pass the event log object into every class. 我不想将事件日志对象传递给每个类。 So do I... 我也是...

1) create local signals in each class, and let my main window connect them all? 1)在每个类中创建本地信号,然后让我的主窗口将它们全部连接起来? 2) can I make the slot static so all views can access it without needing the event log object? 2)我可以将插槽设为静态,以便所有视图都可以访问它而无需事件日志对象吗? 3) create one signal and pass it as a function pointer into each class so they can all use the same signal? 3)创建一个信号并将其作为函数指针传递到每个类中,以便它们都可以使用相同的信号? 4) something else? 4)还有什么?

Thanks. 谢谢。

  1. At most this, see below. 最多见下文。
  2. No. Slots have to be associated with class instances . 否。插槽必须与类实例相关联
  3. I doubt the moc would understand this, and it seems unnecessarily complicated. 我怀疑交通部是否会理解这一点,而且看起来不必要地复杂。
  4. In principle you could let the events propagate up the parent/child hierarchy and let the main window edit the log but this is also too complicated. 原则上,您可以让事件沿父/子层次结构传播,并让主窗口编辑日志,但这也太复杂了。

Assuming your view classes inherit from QAbstractItemView then they already have signals you can use, particularly if you use the Q*Widget convenience classes. 假设您的视图类继承自QAbstractItemView,则它们已经具有可以使用的信号,特别是如果您使用Q * Widget便利类。 In your situation, if this doesn't work for me, I do 1). 在您的情况下,如果这对我不起作用,请执行1)。 You may also consider signaling from the model classes--that is where the updating actually happens after all. 您可能还考虑了来自模型类的信号-毕竟,更新实际上是在这里进行的。

Couldn't you setup static member functions in your event logging classes to retrieve a ptr to an event logging instance? 您不能在事件记录类中设置静态成员函数来检索事件记录实例的ptr吗? Return the one global instance if that's all you have, 如果仅此而已,则返回一个全局实例,

static EventLogger* EventLogger::getLoggerInstance(); 静态EventLogger * EventLogger :: getLoggerInstance();

or a more nuanced if you have multiple event loggers. 如果您有多个事件记录器,则更为细微。

static EventLogger* EventLogger::getLoggerInstance(args, ...); 静态EventLogger * EventLogger :: getLoggerInstance(args,...);

If a view needs to hook in the the event logging, it retrieves an event logging instance and connects to it. 如果视图需要挂入事件日志记录,则它将检索事件日志记录实例并连接到该实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM