Signals and slots


Signals and slots is a language construct introduced in Qt for communication between objects which makes it easy to implement the observer pattern while avoiding boilerplate code. The concept is that GUI widgets can send signals containing event information which can be received by other widgets / controls using special functions known as slots. This is similar to C/C++ function pointers, but signal/slot system ensures the type-correctness of callback arguments.
The signal/slot system fits well with the way graphical user interfaces are designed. Similarly, the signal/slot system can be used for other non-GUI usages, for example asynchronous I/O event notification or to associate timeout events with appropriate object instances and methods or functions. It is easy to use and no registration/deregistration/invocation code need to be written, because Qt's metaobject compiler automatically generates the needed infrastructure.
A commonly used metaphor is a spreadsheet. A spreadsheet has cells that observe the source cell. When the source cell is changed, the dependent cells are updated from the event.

Alternative implementations

There are some implementations of signal/slot systems based on C++ templates, which don't require the extra metaobject compiler, as used by Qt, such as libsigc++, , , , , , , , , and . Common Language Infrastructure languages such as C# also supports a similar construct although with a different terminology and syntax: events play the role of signals, and delegates are the slots. Another implementation of signals exists for , inspired by C# events and signals/slots in Qt. Additionally, a delegate can be a local variable, much like a function pointer, while a slot in Qt must be a class member declared as such. The C based GObject system also provides similar functionality via .
In D it is implemented by .

Libraries

Java: - multi-threaded, type-safe, based on the annotation introduced in Java 8.
C++: - thread-safe, type-safe, written in C++11 with atomic variables.