Search code examples
c++qtqtcoreqt-signalsmoc

Are signals and slots syntactic sugar or there is more to them?


I'm learning Qt and I'm having lots of fun with the signals-slotpattern. The question that I have is that, are signals and slots simply syntactic sugar for event listeners and handlers or what happens in the background is of different nature? If the later, what is the fundamental difference?


Solution

  • Are signals and slots syntactic sugar or there is more to them? The question that I have is that, are signals and slots simply syntactic sugar for event listeners/handlers

    No, the mean reason for their existence is decoupling of the emission and handling.

    or what happens in the background is of different nature?

    The idea is that you separate the emission from the handling of a certain "event". If you consider the direct function calls as an alternative, I would like to point that out, with them, the code responible for the emission would need to be aware of the code that actually handles the "signal". That is the two parts would be too tight too each other.

    It would not be possible to dynamically add another handler for the signal without changing the code that is responsible for the signal emission. Imagine an example like this:

    • Some part of the code emits a signal "fruit arrived"

    • This code would directly call the "wash fruit" method.

    What if someone would like to add a method to count the number of fruits?

    • The previous code would need to be modified to include the direct call to that method.

    With the signal-slot mechanism, you would not need to touch the original code. You could just simple connect your new slot to the existing signal from an entirely different piece of code. This is called good design and freedom.

    This is especially useful when you have libraries like Qt that can emit signals all around without knowing about the Qt applications in advance. It is up to the Qt applications how to handle the signals, on their own.

    In addition, this pattern also makes the application more responsive and less blocking, which would be the case with direction function calls. This is because of the existence of the event loop on which the Qt signal-slot mechanism is built upon. Surely, you could use threading with direct calls, too, but it becomes a lot more work and difficult to maintain than it would be necessary in an ideal world.

    So, as partially already touched, there is a QtEventLoop in the background queuing up these events for processing, although it is possible to execute "direct calls", too.

    The really background internal implementation code can be found there, and in moc (meta object compiler). Moc is basically creating a function for signals which you do not define a body for, so you just declare them in the QObject subclasses when you need it.

    You can read more upon the topic in here, but I think my explanation could get you going:

    Qt Signals & Slots

    QtDD13 - Olivier Goffart - Signals and Slots in Qt 5

    How Qt Signals and Slots Work

    How Qt Signals and Slots Work - Part 2 - Qt5 New Syntax

    Signals and Slots in Qt5

    Using the Meta-Object Compiler (moc)