Signals and slots in Depth
The signal and groove mechanism are the basis for Qt programming. It enables application to help these objects together without knowing any contacts between objects. We have connected some signals and slots, declare our own signals and slots, perform their own slots, and make their own signals. Let us take some time to see this mechanism.
The slot is almost the same as the ordinary C member function. They can be Virtual, OverLoaded, Public, Protected, Private, and can be called directly by other C idiom functions. The difference is that the groove can be connected by a signal, and the signal is sent automatically.
The Connect () declaration is as follows:
Connect (Sender, Signal (Signal), Receiver, Slot (slot);
Sender and Receiver are pointers, Signal, and Slot are function Signatures without parameter names. Signal () and slot () macros will convert their parameters to become a string.
In the example of just now, we always connect different signals to different slots, there are more possibilities to explore:
1. A signal can be connected to many slots:
Connect (Slider, Signal (INT)), SpinBox, Slot (SetValue (INT))); Connect (Slider, Signal (INT)), this, Slot (UpdateStatusbarindicator (int));
When the signal is sent, these grooves are connected to one call to any order.
2. Many signals can be connected to the same groove:
Connect (LCD, Signal (Overflow ()), THIS, SLOT (HandleMatherror ())); Connect (Calculator, Signal (DivisionByzero ()), THIS, SLOT (HandleMatherror ()));
This slot is called when any of the signals are sent.
3. A signal can be connected to another signal:
Connect (LineEdit, Signal (TextChanged (Const Qstring &)), this, Signal (UpdateRecord (Const QString &));
When the first signal is issued, the second signal will also be issued. From this, the SINGNAL-SINGNAL connection cannot distinguish between the Signal-Slot connection.
4. The connection can be removed:
Disconnect (LCD, Signal (overflow ()), this, slot (HandleMatheError ()));
This rarely needs, because QT automatically deletes all connections including objects when the object is deleted.
When a signal is connected to a slot (or connected to another signal), they must use the same parameters in the same order:
Connect (FTP, Signal (RawcommandReply (INT, Const Qstring &)), this, Slot (ProcessReply (int, const qstring ");
Abnormal case, if the signal is more parameters than the slot it is connected, the increasing parameters will be ignored directly:
Connect (FTP, Signal (RawcommandReply (INT, Const Qstring &)), this, Slot (CheckerRrorcode (int));
If the parameter type does not match, or the signal or slot does not exist, QT will output a runtime warning. Similarly, if the parameter name is included in the signal or slot Signatures (signature), Qt will give a warning.