Question

I'm working with some code that I didn't write, and am confused about the practice of, essentially, using signals and slots for absolutely everything.

I understand using them to communicate UP the dependency tree (basically, used as callbacks). For example, if we have the following classes:

MainWindow -> SomeDialog -> SomeView -> SomeModel

... And SomeModel is capable of changing some shared application state, it may emit a signal changed() which is connected to a slot of SomeView, which in turn sends a signal to SomeDialog, etc.

This is valuable because it ensures there is no circular dependency between an object and its child; SomeModel doesn't have to know anything about SomeView or its parents in order to communicate with it.

But why would you do the reverse? That is, if I have AnotherDialog, AnotherView and AnotherModel, and AnotherModel needs to know when the state is changed in SomeModel, why would I use signals to communicate in the downward direction? The path of signal propagation ends up being:

SomeModel -> SomeView -> SomeDialog -> MainWindow -> AnotherDialog -> AnotherView -> AnotherModel

This is difficult to debug, confusing, and (as far as I can tell) completely unnecessary. Once the signal is propagated to MainWindow, it can just call a method on its instance of AnotherDialog directly, there's no need for signals... right?

I realize this question may be overly vague, but I wanted to make sure I'm not ignoring some Qt design principle before I go and refactor all the old code.

Was it helpful?

Solution

The only reason for using signals might be because AnotherDialog, AnotherView and/or AnotherModel have different thread affinities (but even then it might have been preferable to use QMetaObject::invokeMethod()).

If all of these classes exist within a single thread, then there wouldn't be any good reason to use signals.

There do appear to be other design flaws here, but looking at your comments it seems as though you're aware of them.

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