The signal slot mechanism is the basis of QT programming. Through the signal slot, QT components can communicate with each other without knowing the other party. This decouples the relationships between classes to the maximum extent.
There is no big difference between Slot functions and common C ++ member functions. They can also make virtual; can be overwritten; public, protected, or private; can be called by other C ++ functions; parameters can be of any type. The difference is that the slot function can be connected to a signal, which can be called automatically when the signal occurs.
The prototype of the Connect () Statement is similar:
Connect (sender, signal (signal), explorer, slot (slot ));
Here, both sender and explorer are of the qobject type, and Singal and slot are function signatures without parameter names. The singal () and slot () macros are used to convert parameters into strings.
In depth, there are more possible usage of the signal slot, as shown below.
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 )));
Note: In this case, these slots will be called one by one, but their call sequence is uncertain.
Multiple signals can be connected to one slot:
Connect (LCD, signal (overflow ()),
This, slot (handlematherror ()));
Connect (calculator, signal (divisionbyzero ()),
This, slot (handlematherror ()));
This means that this slot will be called as long as any signal is sent.
One signal can be connected to another signal:
Connect (lineedit, signal (textchanged (const qstring &)),
This, signal (UpdateRecord (const qstring &)));
This means that when the first signal is sent, the second signal is sent. In addition, there is no difference between this form of signal-signal and the form of signal-slot.
The slot can be canceled:
Disconnect (LCD, signal (overflow ()),
This, slot (handlematherror ()));
This is not often the case, because after an object is deleted, QT automatically removes all the slots connected to the object.
To correctly connect the signal slot, the number, type, and sequence of parameters of the signal and slot must be the same. For example:
Connect (FTP, signal (rawcommandreply (INT, const qstring &)),
This, slot (processreply (INT, const qstring &)));
Here is an exception. If the signal parameter is greater than the slot parameter, the parameters after this parameter will be ignored. For example:
Connect (FTP, signal (rawcommandreply (INT, const qstring &)),
This, slot (checkerrorcode (INT )));
Here, the const qstring & parameter will be ignored by the slot.
If the parameter of the signal slot is incompatible, or the signal or slot does not exist, or the parameter name appears in the connection of the signal slot, during compilation in debug mode, qt can intelligently give warnings.
Before that, we only used the signal slot in widgets. However, we noticed that the connect () function is actually implemented in qobject and is not limited to Gui. Therefore, as long as we inherit the qobject class, we can use the signal slot mechanism:
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;
};
In use, we provide the followingCode:
Void EMPLOYEE: setsalary (INT newsalary)
{
If (newsalary! = Mysalary ){
Mysalary = newsalary;
Emit salarychanged (mysalary );
}
}
In this way, when setsalary () is called, The salarychanged () signal is sent. Note the if judgment here. This is a method to avoid recursion! Do you still remember the loop connection mentioned above? If no if clause is available, infinite recursion is generated when a loop connection occurs.
Reprinted statement:This article from http://blogold.chinaunix.net/u3/94700/showart_2384178.html