Qt Signal Slot Observer Pattern

  1. Sep 12, 2004  The article describes an efficient way to implement delegates in C using Signal and Slot pattern. Download source files - 4.81 Kb; Introduction. In my previous article “Generic Observer Pattern and Events in C”, we discussed classical “Observer Pattern” and its implementation in C using templates. An event class CppEvent introduced in that article allowed us to bind together.
  2. The Observer pattern is a mechanism for notifying changes of state in one object to one or more other objects. In Qt programming, it is seldom used, because signals and slots fulfill that role very well. Instead of emitting signals, an observable class calls virtual functions on a list of 'observers' (or 'listeners').

Signal and Slots Signals and slots is a language construct introduced in Qt, which makes it easy to implement the Observer pattern while avoiding boilerplate code. The concept is that controls (also known as widgets) can send signals containing event information which can be received by other controls using special functions known as slots.

I'm interested in finding from others how you have solved a pretty simple problem.

I have many QObjects and QWidgets. Each of these has a data buffer, varying in type depending on what information it is to hold. All data comes from a single repository but specific data gets farmed out to these QObjects and QWidgets.

At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

Signal

I've thought about having a base class similar to the following pseudo-code:

Qt Signal Slot Thread

Then when I want to clear ALL buffers, I just need to emit clearBuffers from the BaseClassController

Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this? Or am I barking completely up the wrong tree?

Thanks

Qt Signal Slot Connect

Qt signals and slots tutorial

Coming from other parts of computer science (say HPC) I have not much knowledge about Qt, nevertheless I am working on some project with a tiny toy-QT Frontend.

Years ago I learnt about the observer pattern, where we usually have some class (the subject) which is observed by some observer. The observers register to the subject, and if the subject does some action, e.g., sets some new value, it notifies the observers.

Mapping this to QT mechanisms, we have slots instead of observers, which create a connection to the signal. When the object with the signal does some action, it emits the signal and the registered slots are notified.

But now my question about what is good practice:

  • Assume we have a SpinBox which stores some value with has some meaning. This value is used at various places, but still, the value is only changed by the SpinBox. How would we model this? I would still use some third object (clearly named) which stores the value (and has one signal). The SpinBox and all other objects would need to register to this object.
  • Assume we have a SpinBox, a TextBox and probably some programmatically determined way to set some value. So the value is changed and read at various places. We could either define one of them as 'the central place of storage', or we could again have some other object with one signal.
  • Assume we have three values A, B, and C. All of them are read and written at different places; sometimes I change one, some times I cange all values. When I change some value (or all of them), I start an expensive calculation. We could now use three observers (problem: I restart the expensive calculation too often when all values are changed at once), or we could use one observer for all (problem: I change values which I don't need to), or I have again one central place of storage for all three values A, B, C, and four signals (set A, set B, set C, and set ABC).

Qt Signal Slot Example

In my opinion I would always vote for the independent class with one signal. But when I look through some codes I often see that this is done kind of randomly.

Qt Signal Slot Parameter

Is there some common sense and some reasoning why to choose one or another approach in both cases?