User Tools

Site Tools


qt:signals_and_slots

This is an old revision of the document!


Signals and slots

Events are handled in Qt by a signal and slots system. Signals are emitted by objects (in response to some stimuli or whatnot) and are received by slots in other objects. Connections are made between signals and slots by the programmer using some simple non-C++ code foo. The behavior associated with a received signal is determined programatically.

Making connections

A connection between a signal and a slot is made as follows:

QObject::connect({pointer to emiter}, SIGNAL({signal-name}({parameter-type(s)})),
                 {pointer to receiver}, SLOT({slot-name}({parameter-type(s)})));

The emiter emits the signal {signal-name} with a list of values whose types are identified with {parameter-types(s)}. The receiver receives this signal into its {slot-name} slot along with the values emitted with the signal. The slot then takes the appropriate action depending on the value of the passed parameters.

For example:1)

QSpinBox* spinBox = new QSpinBox;
QLabel* label = new QLabel("0");
.
.
.
QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                 label,   SLOT(setNum(int)));

The valueChanged signal is defined for QSpinBox and the setNum slot is defined for QLabel. valueChanged emits an int, and setNum expects to receive an int. Whenever spinBox's value changes (e.g., in response to user interaction) it will emit the valueChanged signal along with its updated value (as an int). label's setNum receives the signal and updates itself to show the (int) value received.

Multiple connections

Signals may connect to multiple slots. (FIXMEIs the reverse true?)

If a signal connects to multiple slots, the execution order of the slots is undefined.

Writing your own slots

A number of slots and their behaviors are defined for many of the classes that make up Qt. It is also possible to implement new (or override existing :?:) slots.

Writing your own signals

A number of signals are defined for many of the classes that make up Qt. It is also possible to implement new (or override existing :?:) signals.

1)
From Molkentin, p. 37
qt/signals_and_slots.1291904862.txt.gz · Last modified: 2010/12/09 14:27 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki