[IGSTK-Developers] QT : Signals and Slots

Luis Ibanez luis.ibanez at kitware.com
Thu Oct 20 18:45:52 EDT 2005


As a follow up on the discussion about the refactoring of the state
machine. Here is one of the topics that we talked about during
yesterday's tcon:

                QT: Signals and Slots

as David pointed out, the new proposed model of communication
for IGSTK state machines is similar to the Qt Signals and Slots
mechanism. The following page describe the rationale behind the
design pattern used in Qt.


-----


        http://doc.trolltech.com/3.3/signalsandslots.html


( Here there is some copy/pasted text from the link above,
   but it is better to read it from the link because there
   are nice explanatory diagrams there )



QT: Signals and Slots

Signals and slots are used for communication between objects. The
signal/slot mechanism is a central feature of Qt and probably the part
that differs most from other toolkits.

In GUI programming we often want a change in one widget to be notified
to another widget. More generally, we want objects of any kind to be
able to communicate with one another. For example if we were parsing an
XML file we might want to notify a list view that we're using to
represent the XML file's structure whenever we encounter a new tag.

Older toolkits achieve this kind of communication using callbacks. A
callback is a pointer to a function, so if you want a processing
function to notify you about some event you pass a pointer to another
function (the callback) to the processing function. The processing
function then calls the callback when appropriate. Callbacks have two
fundamental flaws. Firstly they are not type safe. We can never be
certain that the processing function will call the callback with the
correct arguments. Secondly the callback is strongly coupled to the
processing function since the processing function must know which
callback to call.

  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 pre-defined signals, but we can always subclass to add
our own. A slot is a function that is called in reponse to a particular
signal. Qt's widgets have many pre-defined slots, but it is common
practice to add your own slots so that you can handle the signals that
you are interested in.

The signals and slots mechanism is type safe: the signature of a signal
must match the signature of the receiving slot. (In fact a slot may have
a shorter signature than the signal it receives because it can ignore
extra arguments.) Since the signatures are compatible, the compiler can
help us detect type mismatches. Signals and slots are loosely coupled: a
class which emits a signal neither knows nor cares which slots receive
the signal. Qt's signals and slots mechanism ensures that if you connect
a signal to a slot, the slot will be called with the signal's parameters
at the right time. Signals and slots can take any number of arguments of
any type. They are completely typesafe: no more callback core dumps!

All classes that inherit from QObject or one of its subclasses (e.g.
QWidget) can contain signals and slots. Signals are emitted by objects
when they change their state in a way that may be interesting to the
outside world. This is all the object does to communicate. It does not
know or care whether anything is receiving the signals it emits. This is
true information encapsulation, and ensures that the object can be used
as a software component.

Slots can be used for receiving signals, but they are also normal member
functions. Just as an object does not know if anything receives its
signals, a slot does not know if it has any signals connected to it.
This ensures that truly independent components can be created with Qt.

You can connect as many signals as you want to a single slot, and a
signal can be connected to as many slots as you desire. It is even
possible to connect a signal directly to another signal. (This will emit
the second signal immediately whenever the first is emitted.)

Together, signals and slots make up a powerful component programming
mechanism.






More information about the IGSTK-Developers mailing list