Signals and slots are an important part of QT programming. This mechanism can associate the actions of objects without mutual understanding. In the previous routines, We have connected the signal and slot, declared the signal and slot of the control, implemented the slot function, and sent the signal. Now let's have a better understanding of this mechanism.
The slot is similar to a common C ++ member function. They can be virtual functions, overloads, public functions, proteve ve, or private functions ), they can be called like any c ++ member function and can pass any type of parameters. The difference is that a slot function can be connected to a signal. As long as the signal is sent, this slot function will be automatically called.
The connect function syntax is as follows:
Connect (sender, signal (signal), explorer, slot (slot ));
Sender and consumer er are qobject object pointers, while signal and slot are function prototypes without parameters. The functions of signale () and slot () macros are to convert them into strings.
In the current example, we have connected different signals and slots. The following rules must be taken into account in actual use:
1. One signal can be connected to multiple slots:
Connect (slider, signal (valuechanged (INT), spinbox, slot (setvalue (INT )));
Connect (slider, signal (valuechanged (INT), this, slot (updatestatusbarindicator (INT )));
When the signal is sent, the slot function is called, but the call sequence is random and uncertain.
2. Multiple signals can be connected to one slot
Connect (LCD, signal (overflow (), this, slot (handlematherror ()));
Connect (calculator, signal (divisionbyzero (), this, slot (handlematherror ()));
The slot function is executed when any signal is sent.
3. One signal can be connected to another signal
Connect (lineedit, signal (textchanged (const qstring &)),
This, signal (UpdateRecord (const qstring &)));
After the first signal is sent, the second signal is also sent. In addition, the signal and the signal connection are the same as the signal and the slot connection.
4. The connection can be deleted.
Disconnect (LCD, signal (overflow (), this, slot (handlematherror ()));
This function is rarely used. After an object is deleted, QT automatically deletes all connections of this object.
The signal and the slot function must have the same parameter type so that the signal and the slot function can be connected successfully:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (processreply (INT, const qstring &)));
If the number of parameters in the signal is greater than that of the slot function, extra parameters are ignored:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (checkerrorcode (INT )));
If the parameter speed type does not match, or the signal and slot do not exist, QT will give a warning during the running of the debug status. If the parameter name is included in the connection between the signal and the slot, QT will give a warning.
In the previous examples, the signals and slots of controls are used. However, the signal and slot mechanisms are implemented in qobject and can be implemented in any subclass inherited from qobject.
Class EMPLOYEE: Public qobject
{
Q_object
Public:
Employee () {mysalary = 0 ;}
Int salary () const {return mysalary ;}
Public slots:
Void setsalary (INT newsalary );
Signals:
Void salarychanged (INT newsalary );
PRIVATE:
Int mysalary;
};
Void EMPLOYEE: setsalary (INT newsalary)
{
If (newsalary! = Mysalary ){
Mysalary = newsalary;
Emit salarychanged (mysalary );
}
}
Note: Only newsalary! = Mysalary.
××××××××××××××××××××××××××××××××××××××××××× ××××××××××××××××××××××××××××××××××××××××××× ×××
Qt meta-Object System
One major success of QT is the expansion of C ++, that is, connecting independent software modules without any details between modules.
This mechanism is the meta-Object System, which provides two key purposes: Signal and slot and introspection (introspection ). The introspection function allows the application to obtain the "meta-information" of its subclass of qobjec during runtime, which is necessary for implementing signals and slots, including the list of all signals and slots, and class name. This mechanism also supports attributes (used in QT designer) and Text Translation (internationalization. These constitute the basis of QSA (QT script for application.
Standard C ++ does not provide the dynamic meta-information required by the QT meta-Object System. Qt provides an independent tool MOC to implement the transformation from a q_object macro to a C ++ function. MOC is implemented in pure C ++, so it can be used in any c ++ compiler.
This mechanism works as follows:
Q_object declares the internal functions that must be implemented by some qobject sub-classes: metaobject (), TR (), qt_metacall (), etc.
The MOC tool of QT implements the q_object macro acoustic function and all signals.
The qobject member functions connect () and disconnect () Use these introspection functions to connect signals and slots.
These are automatically processed through qmake, MOC, and qobject. Programmers generally do not need to consider them. If you are curious about this, you can view the qmetaobject Class documentation and the C ++ Code implemented by MOC.