Signals and slots

{{main article|Qt (framework)}}

Signals and slots is a language construct introduced in Qt{{cite web |url=http://qt-project.org/doc/qt-5.1/qtcore/signalsandslots.html |archive-url=https://web.archive.org/web/20131010211929/http://qt-project.org/doc/qt-5.1/qtcore/signalsandslots.html |url-status=dead |archive-date=October 10, 2013 |title=Signals & Slots - QtCore 5.1 |publisher=Qt Project |date=2013-07-04 |accessdate=2013-07-04 }} for communication between objects which makes it easy to implement the observer pattern while avoiding boilerplate code. The concept is that GUI widgets, and other objects, can send signals containing event information which can be received by other objects using special member functions known as slots. This is similar to C/C++ function pointers, but the signal/slot system ensures the type-correctness of callback arguments.{{Citation needed|date=January 2018}}

The signal/slot system fits well with the way graphical user interfaces are designed.{{citation needed|date=January 2020}} Similarly, the signal/slot system can be used for other non-GUI usages, for example asynchronous I/O (including sockets, pipes, serial devices, etc.) 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 (MOC) automatically generates the needed infrastructure.

A spreadsheet programs update system could be implemented using signals and slots such that when a cell is changed, its dependent cells are notified/updated.

Alternative implementations

{{cleanup-section|reason=Too many alternative implementations without describing what signal/slot is|date=July 2013}}

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++, [http://sigslot.sourceforge.net/ sigslot], [https://vdksoft.github.io/signals/index.html vdk-signals], [https://github.com/NoAvailableAlias/nano-signal-slot nano-signal-slot], [http://i42.co.uk/stuff/neosigslot.htm neosigslot], [https://github.com/pbhogan/Signals Signals], [http://www.boost.org/doc/html/signals2.html boost.signals2], [https://zajo.github.io/synapse/ Synapse], [https://code.google.com/p/cpp-events Cpp::Events], [https://web.archive.org/web/20100124022807/http://pt-framework.sourceforge.net/sigslot.html Platinum], [https://web.archive.org/web/20090511001206/http://www.newplanetsoftware.com/jx/mvc.php JBroadcaster] and [https://github.com/KDAB/KDBindings KDBindings]. Common Language Infrastructure (CLI) 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 [https://github.com/robertpenner/as3-signals/ ActionScript 3.0], 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 [http://developer.gnome.org/gobject/stable/gobject-Signals.html GSignal].

In D it is implemented by [https://dlang.org/phobos/std_signals.html std.signals].

See also

Libraries

Java: [https://github.com/retuxx/sig4j sig4j] - multi-threaded, type-safe, based on the [https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html FunctionalInterface] annotation introduced in Java 8.

C++: [https://vdksoft.github.io/signals/index.html vdk-signals] - thread-safe, type-safe, written in C++11 with atomic variables.

References

{{reflist|30em}}

{{Qt}}

Category:Qt (software)