Signals and slots

Signals and slots are a concept from the programming. You realize an event-driven program flow or an event-driven communication between program objects. The term was originally coined by the Qt library, but is now used by a number of other libraries. The concept can be regarded as an application of the design pattern Observer. It is used as an alternative to direct callback functions (English callbacks ) if this proves to be too inflexible or not type-safe (eg in the development of complex graphical applications).

Overview

Signals are "messages" that are released when an event occurs (issued). A slot is basically a normal function, which can be linked in a particular way with a signal. Slots and signals "know" anything about each other first. Only through the connection is formed, the actual logic, every time when the signal is outputted, then the related slot is called. A signal may also be connected with a plurality of slots, so that several functions are invoked when an event occurs. Also, a slot to be connected to a plurality of signals, which have the same function in different events occur is called.

Particularly common to find signal-slot mechanisms in program libraries for creating graphical user interfaces. Here they perform the task objects, in particular to link the controls together and thus to fill the control interface elements such as buttons and list boxes with functionality.

The C library Qt is probably the most famous example of the use of signals and slots. Realized they are there through new to C introduced keywords like signal, slot and emit that before they are removed by the C preprocessor, the Qt - own " Meta Object Compiler " ( moc ) help you to create additional C code.

There are also implementing various program libraries, which signal-slot mechanisms by means of generic programming (templates) and functors, such as the library Boost or libsigc , a signal-slot library which is used for example by the gtkmm library.

Benefits

Signals and slots are simple and flexible to be used in compliance with the type-safety as callbacks, but this is due to the overhead slightly at the expense of speed. However, the difference is hardly relevant in practice.

The syntax of signals and slots can often seem easier than the method pointer for callbacks or necessary.

Examples with Qt

Predefined signals and slots

Qt included many classes already have several pre-defined signals, and slots which can be used. The following example is intended to show how you can use this to program basic functions in a GUI.

MainWindow :: MainWindow ( QWidget * parent, Qt :: wFlags flags): QMainWindow (parent, flags) { ui.setupUi (this);   QWidget * w = new QWidget (); QCheckBox * c = new QCheckBox ( " visible window ", this);   connect ( c, SIGNAL ( clicked ( bool) ), w, SLOT ( setVisible (bool ))); } You can see the constructor of the main window. Ui.setupUi line (this); used to generate the user interface and is not described in detail here (see Qt documentation). The following two lines are used to create an empty window widget w and a "visible window " labeled with the text box widgets c, which appears in the main window.

The connection between the two objects is then achieved by means connect. As a signal, the signal clicked the checkbox that is emitted when the user clicks the box serves. Status after clicking, so if the box is checked or not, is transferred as a Boolean parameter. setVisible is a slot, can be controlled in all the boxes on the whether it is visible or not. SIGNAL and SLOT are Qt own keywords to characterize signals and slots as such; they are recognized by the meta -object compiler.

After starting the program, the second, empty window now would each appear or disappear each time you click the checkbox.

Example of self-produced signals and slots

At a simple class shows how signals and slots work in the Qt library. The class number, and stores a value of two functions, to change or to read the value:

/ / Zahl.h   # include   class number: public QObject {      Q_OBJECT   private:      int value;   public:      Number (): value ( 0)      {      }        read int value () const {return value; }   public slots:      void set value ( int value )      {          if ( value! = this-> value)          {              this-> value = value;              emit wertGeaendert (value);          }      }   signal:      void wertGeaendert (int newValue ); };

/ / Main.cpp   # include   int main ( void) {    Number a, b; / / A.wertLesen () == 0, b.wertLesen () == 0      a.setzeWert (5); / / A.wertLesen () == 5, b.wertLesen () == 0    b.setzeWert (48); / / A.wertLesen () == 5, b.wertLesen () == 48      QObject :: connect ( & a, SIGNAL ( wertGeaendert (int) ), & b, SLOT (set value (int )));      a.setzeWert (12); / / A.wertLesen () == 12, b.wertLesen () == 12    b.setzeWert (23); / / A.wertLesen () == 12, b.wertLesen () == 23 } The number of classes derived from the class QObject that contains the necessary functions for the use of signals and slots. Q_OBJECT is a macro that must be included in all classes in which signals or slots are declared. The set value () function is declared as a slot, that is, it can be connected to signals such as wertGeaendert (). The meta -object compiler generates from this declaration of the class number of additional source code, which contains functions to use the newly declared signals and slots. For the signals no longer functions must be implemented by the programmer.

When signals are activated with emitted, all slots that were connected during the duration of the program with this signal, called. This combination of signals and slots is done with the connect ( ) ( see, for better understanding links: Qt documentation for signal and slots) and can also be canceled. In connect () also checks whether the specified signals and slots exist and whether the types of the parameters match. The associated signals and slots (each instance of a class and a function ) are stored in a list. A signal can be assigned any number of slots.

In the example, the entities A and B obtained in the initialization value of 0 with a.setzeWert (5); and b.setzeWert (48); the value of a and b is changed and each of the signal wertGeaendert ( ) is activated. Since these first call set value () no slot is still connected with the signals of both instances, the activation of signal does nothing. Only after connecting the signal a.wertGeaendert () with the slot b.setzeWert ( ) with connect () is with a change in the value of a the b.setzeWert () function is called and thus changed the value of b. B.setzeWert The last call (23 ) has no effect on a, since the signal b.wertGeaendert () is connected no slot.

Implementations

  • The signal and slots - Model of Qt, a C - class library ( for cross-platform user interface programming )
  • C : Boost.Signals2 or Boost.Signals (deprecated)
  • C : libsigc - Template -based.
  • C : sigslot
  • C : XLObject - Template -based, Qt - oriented.
  • C : signal
  • C: libevent - multi-threaded, multi-platform.
  • C: libev - minimalist alternative to libevent.
  • PHP: TYPO3 Flow - application framework with signal-slot support
730078
de