Signals and slots
Encyclopedia
Signals and slots is a language construct introduced in Qt
, which makes it easy to implement the Observer pattern
while avoiding boilerplate code
. The concept is that controls (also known as widgets) can send signals containing event information (e.g. the text "toto" was selected, the scrollbar has been adjusted to value 37) which can be received by other controls using special functions known as slots. The signal/slot system fits well with the way Graphical User Interface
s are designed. Similarly, the signal/slot system can be used for asynchronous I/O
(including sockets, pipes, serial devices, etc.) event notification or to associate timeout events with appropriate object instances and methods or functions. No registration/deregistration/invocation code need be written, because Qt's Meta Object Compiler (MOC) automatically generates the needed infrastructure.
templates, which don't require the extra Meta Object Compiler, as used by Qt, such as libsigc++
, sigslot, Signals, boost.signals, Cpp::Events, Platinum and JBroadcaster. The C# language 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, 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. Due to the language limitation, there isn't a common implementation for ANSI C, but an environment dependent method was given out, see c-sigslot, it has been tested on several platforms, notably the ARM C Compiler, but should support any standard ANSI C compiler.
Qt (toolkit)
Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface , and also used for developing non-GUI programs such as command-line tools and consoles for servers...
, which makes it easy to implement the Observer pattern
Observer pattern
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods...
while avoiding boilerplate code
Boilerplate code
In computer programming, boilerplate is the term used to describe sections of code that have to be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs...
. The concept is that controls (also known as widgets) can send signals containing event information (e.g. the text "toto" was selected, the scrollbar has been adjusted to value 37) which can be received by other controls using special functions known as slots. The signal/slot system fits well with the way Graphical User Interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
s are designed. Similarly, the signal/slot system can be used for asynchronous I/O
Asynchronous I/O
Asynchronous I/O, or non-blocking I/O, is a form of input/output processing that permits other processing to continue before the transmission has finished....
(including sockets, pipes, serial devices, etc.) event notification or to associate timeout events with appropriate object instances and methods or functions. No registration/deregistration/invocation code need be written, because Qt's Meta Object Compiler (MOC) automatically generates the needed infrastructure.
Alternative implementations
There are some implementations of signal/slot systems based on C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
templates, which don't require the extra Meta Object Compiler, as used by Qt, such as libsigc++
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 using C++ template metaprogramming...
, sigslot, Signals, boost.signals, Cpp::Events, Platinum and JBroadcaster. The C# language also supports a similar construct although with a different terminology and syntax: events play the role of signals, and delegates
Delegate (.NET)
A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners....
are the slots. Another implementation of signals exists for 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
Function pointer
A function pointer is a type of pointer in C, C++, D, and other C-like programming languages, and Fortran 2003. When dereferenced, a function pointer can be used to invoke a function and pass it arguments just like a normal function...
, while a slot in Qt must be a class member declared as such. Due to the language limitation, there isn't a common implementation for ANSI C, but an environment dependent method was given out, see c-sigslot, it has been tested on several platforms, notably the ARM C Compiler, but should support any standard ANSI C compiler.
External links
- Signals and slots, from Qt documentation.
- Signals and slots, from Boost documentation.