0

Using MVC, I have several view classes, all of which need to write to an event log. The event log contains a slot called addEntry which writes the data to the log. 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? 2) can I make the slot static so all views can access it without needing the event log object? 3) create one signal and pass it as a function pointer into each class so they can all use the same signal? 4) something else?

Thanks.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
kkeogh
  • 47
  • 1
  • 6
  • I'm not sure what you mean with #2. You can put a signal or property in a base class and all derived classes will have it. A 'static' signal wouldn't work. It needs to have an instantiated object that is to receive the signal. – Jay Mar 18 '14 at 23:42
  • Currently my views do not all derive from the same base class, but maybe it's something I should consider. – kkeogh Mar 19 '14 at 15:29

2 Answers2

0
  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. In your situation, if this doesn't work for me, I do 1). You may also consider signaling from the model classes--that is where the updating actually happens after all.

Community
  • 1
  • 1
Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
0

Couldn't you setup static member functions in your event logging classes to retrieve a ptr to an event logging instance? Return the one global instance if that's all you have,

static EventLogger* EventLogger::getLoggerInstance();

or a more nuanced if you have multiple event loggers.

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

Digikata
  • 1,821
  • 17
  • 25
  • I was trying to avoid forcing all the views to know about the Event Logger. Eventually I'll be writing plugins for the software, and I wanted those classes to be able to send signals without having to worry about the slots. – kkeogh Mar 19 '14 at 15:31
  • Ah if that's of large importance there are things you can to to make the logging setup a bit more generic (e.g. have a event source implementation as a mixin multiple inheritance class), but essentially I'd stay with #1 and ignore this answer. – Digikata Mar 19 '14 at 16:59