A: Signals and slots mechanism is very important part of Qt framework. It has been used for communication between objects of any kind.
Suppose we have two object, obA and obB.
On modification of obA, if we want to notify or call a function of obB, then we use signal and slot concept. Here called function of obB is known as slot.
A slot is a function that is called in response to a particular signal. A slot can be used as a normal function. But for a good coding practice, don’t use it as a normal method unless it is really required.
Signal may be a custom signal or a predefind Qt signal.
Connection of signal and slot can be done as given below:
Connect(object1, SIGNAL(signal()), object2, SLOT(slot()))
Here, signal will get emitted from object1 and can be captured in object2 and call slot() function of object2.

One signal may connect to more than one slot and vice versa as shown in the picture. Please look at all connect method in the above picture.
It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)
Important Ponits:
The signature of the signal must match with signature of the slots. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)
There should not be any parameter name in the signature.
Example:
Connect(object1, SIGNAL(clicked(bool)), object2, SLOT(slot(bool)) => correct
Connect(object1, SIGNAL(clicked(bool status)), object2, SLOT(slot(bool status)) => Incorrect, it will not call slot(bool status) on emitting of signal clicked(bool status)
If several slots are connected to one signal, the slots will be executed one after the other, in an arbitrary order, when the signal is emitted.
function pointer cannot be a signal or slot parameter
Nested class cannot have signals or slots
For more please visit:
http://doc.trolltech.com/4.5/signalsandslots.html
No comments:
Post a Comment