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 engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by ca ...
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 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 interface The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
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 transmission has finished. A name used for asynchronous I/O in the Windows API is overlappe ...
(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++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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 implemented usi ...

sigslotnano-signal-slotSignalsSynapseCpp::EventsJBroadcaster
an
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
delegate Delegate or delegates may refer to: * Delegate, New South Wales, a town in Australia * Delegate (CLI), a computer programming technique * Delegate (American politics), a representative in any of various political organizations * Delegate (United ...
s are the slots. Another implementation of signals exists fo
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 A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poin ...
, while a slot in Qt must be a class member declared as such. The C based GObject system also provides similar functionality vi
GSignal
In D it is implemented b


See also

*
Observer pattern In software design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by ca ...
*
Signal programming Signal programming is used in the same sense as dataflow programming, and is similar to event-driven programming. The word signal is used instead of the word dataflow in documentation of such libraries as Qt, GTK+ and libsigc++. The time instan ...
*
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 th

annotation introduced in Java 8. C++

- thread-safe, type-safe, written in C++11 with atomic variables.


References

{{Qt Qt (software)