People who have used QT know that signal/slot is an efficient communication interface between QT objects and their derived objects. It is the core feature of QT, it is also important to distinguish it from other tool kits. It is completely independent from the Standard C/C ++ language. Therefore, to properly process signals and slots, you must use a QT tool to become a MOC (meta object compiler, this tool is a C ++ preprocessingProgramTo automatically generate high-level events.Code.
1. Different from the callback function:
The callback function passes a function pointer, which can easily cause program crash. On the other hand, the callback method is closely bound to the functional elements of the graphic user interface, so it is difficult to develop independent classification. The signal/slot mechanism can carry any number of parameters and does not generate core dumps as the callback function does.
In addition, you can connect n signals to a single slot, or connect n slots to a single signal, or even one signal to another. In this way, when a signal is sent, the signals or slots connected to it will be executed in a certain order (there is no predetermined order, that is, the execution order is random, emit returns only after all signals and slots connected to it are returned.
2. Signal definition:
Siganls:
Void mysignal ();
Void mysignal (int x );
Void mysignal (int x, int y );
Signals is the key word of QT, rather than the key word of C/C ++. In addition, the difference between a signal and a general function is that all of its return values are void, and it does not have a function implementation body, and its function body is automatically generated by MOC.
3. slot definition:
Public slots:
Void myslot ();
Void myslot (int x );
Different types of slots have different operation permissions, depending on whether the slot is public, protected, or private.
4. Signal and signal or connection to the slot:
Qobeject: connect (obj1, signal (mysignal (), obj2, slot (myslot ()));
Qobeject: connect (obj1, signal (mysignal (), obj2, signal (mysignal2 ()));
5. Signal and slot disconnection:
Qobeject: disconnect (obj1, signal (mysignal (), obj2, slot (myslot ()));
Qobeject: disconnect (obj1, signal (mysignal (), obj2, signal (mysignal2 ()));
This mechanism is very convenient for the operation of GUI controls. Of course, it should be appropriate, standardized, and scientific.