Table of Contents
Signals and slots
Communication between objects in Qt is typically handled by a signal and slots system. Signals are emitted by objects in response to user interaction or other stimuli, and these signals are received by slots in other objects. Connections are made between signals and slots by the programmer using some Qt macro foo. The behavior associated with a received signal is determined programatically. Lots of Qt classes have predefined signals and slots. You can implement new signals and slots in classes you derive yourself.
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.
QSpinBox* spinBox = new QSpinBox; QLabel* label = new QLabel("0"); . . . QObject::connect(spinBox, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
valueChanged signal is defined for QSpinBox and the
setNum slot is defined for QLabel.
valueChanged emits an
setNum expects to receive an
spinBox's value changes (e.g., in response to user interaction) it will emit the
valueChanged signal along with its updated value (as an
setNum receives the signal, which causes
label update itself and show the (
int) value received.
The number of parameters emited by a signal is allowed to be greater than the number of parameters received by a slot; extra parameters are ignored.
Errors in connecting signals and slots are not reported in compiling or linking. However, they will produce a runtime warning:
Object::connect: Incompatible sender/receiver arguments YourClass::aSignal(char,double) –> AnotherClass::aSlot(double)
Signals may connect to multiple slots. Multiple signals may also connect to a single slot.
In the case of multiple connections, the execution order of the slots or signals 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.
Some examples are here.