Question

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.

Was it helpful?

Solution

  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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top