Signals And Slots
   HOME

TheInfoList



OR:

Signals and slots is a language construct introduced in Qt for communication between objects which makes it easy to implement the
observer pattern In software design and software engineering, the observer pattern is a software design pattern in which an object, called the ''subject'' (also known as ''event source'' or ''event stream''), maintains a list of its dependents, called observers (a ...
while avoiding
boilerplate code In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boile ...
. 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. The signal/slot system fits well with the way
graphical user interface A graphical user interface, or GUI, is a form of user interface that allows user (computing), users to human–computer interaction, interact with electronic devices through Graphics, graphical icon (computing), icons and visual indicators such ...
s are designed. Similarly, the signal/slot system can be used for other non-GUI usages, for example
asynchronous I/O In computer science, asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the I/O operation has finished. A name used for asynchronous I/O in the Windows API is '' over ...
(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

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++ libsigc++ is a C++ library for typesafe callbacks. libsigc++ implements a callback system for use in abstract interfaces and general programming. libsigc++ is one of the earliest implementations of the signals and slots concept implement ...
, sigslot, vdk-signals, nano-signal-slot, neosigslot, Signals, boost.signals2, Synapse, Cpp::Events, Platinum, JBroadcaster and 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
ActionScript 3.0 ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (meaning ...
, inspired by C# events and signals/slots in Qt. Additionally, a delegate can be a local variable, much like a
function pointer A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments ...
, while a slot in Qt must be a class member declared as such. The C based GObject system also provides similar functionality via GSignal. In D it is implemented by std.signals.


See also

*
Observer pattern In software design and software engineering, the observer pattern is a software design pattern in which an object, called the ''subject'' (also known as ''event source'' or ''event stream''), maintains a list of its dependents, called observers (a ...
*
Signal programming SIGNAL is a programming language based on synchronized dataflow (flows + synchronization): a process is a set of equations on elementary flows describing both data and control. The SIGNAL formal model provides the capability to describe systems ...
*
Delegate (CLI) A delegate is a form of type-safe function pointer used by the Common Language Infrastructure (CLI). Delegates specify a method to call and optionally an object to call the method on. Delegates are used, among other things, to implement callbacks ...


Libraries

* Java: sig4j - multi-threaded, type-safe, based on the FunctionalInterface annotation introduced in
Java 8 The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community P ...
. * C++: vdk-signals - thread-safe, type-safe, written in
C++11 C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
with atomic variables.


References

{{Qt Qt (software)