Signal and slot are an important part of QT programming. This mechanism can associate the actions of objects without mutual understanding.
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. This task is implemented by the connect function.
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.
Rules for using signals and slots:
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.
5. The signal and slot functions must have the same parameter type so that the signal and slot functions 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.