Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are signals and slots?

Can someone explain in simple terms the "signals and slots" pattern?

like image 842
JeffV Avatar asked Nov 23 '08 20:11

JeffV


People also ask

What are signal and slots in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

What are signals and slots in Python?

Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system .

What are slots in programming?

In computers, a slot, or expansion slot , is an engineered technique for adding capability to a computer in the form of connection pinholes (typically, in the range of 16 to 64 closely-spaced holes) and a place to fit an expansion card containing the circuitry that provides some specialized capability, such as video ...

What is a signal slot C++?

A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them. A slot's arguments cannot have default values, and, like signals, it is rarely wise to use your own custom types for slot arguments.


2 Answers

Signals and slots are a way of decoupling a sender (the signal) and zero or more receivers (the slots). Let's say you a system which has events that you want to make available to any other part of the system interested in those events. Rather than hard-wiring the code that generates event to the code that wants to know about those events, you would use a signals and slots pattern.

When the sender signals an event (usually by calling the function associated with that event/signal) all the receivers for that event are automatically called. This allows you to connect and disconnect receivers as necessary during the lifetime of the program.

Since this question was tagged C++, here is a link to the Boost.Signals library which has a much more thorough explanation.

like image 173
Ferruccio Avatar answered Sep 18 '22 07:09

Ferruccio


I think one can describe signals and slots best when you are looking at them as a possible implementation vehicle for the Observer Pattern or Publish/Subscriber Pattern. There is one signal, for example buttonPressed(IdType) on the Publisher Side. Whenever the button is pressed, all slots that are connected to that signal are called. Slots are on the Subscriber Side. A slot could for example be sendMail(IdType) .

Along with the event "button pressed", the slot would know which button was pressed, since the id would have been handed over. IdType represents the type of the data sent over the connection between the Publisher and the Subscriber. An operation possible for the Subscriber would be connect(signal, slot) which could connect buttonPressed(IdType) with sendMail(IdType), so that if the button is pressed, that particular slot is called.

The good thing about this is that the subscriber (the slot side) doesn't need to care about details of the signal. It just needs to connect. Thus, here we have a great deal of loose coupling. You can change the buttons implementation, but the interface for the slots would still be the same.

Look at Qt Signals/Slots or Boost Signals for more informations.

like image 32
Johannes Schaub - litb Avatar answered Sep 19 '22 07:09

Johannes Schaub - litb