In-depth understanding of signals and slots

Source: Internet
Author: User
The signal slot mechanism is the basis of QT programming.
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 to connect (sender, signal (signal), aggreger, slot (slot). Here, both sender and aggreger are of the qobject type, singal and slot are function signatures without parameter names.
The singal () and slot () macros are used to convert parameters into strings.

Signal and slot usage:

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); note that in this case, these slots will be called one by one, however, their call sequence is uncertain. 2. Multiple signals can be connected to one slot:Connect (LCD, signal (overflow (), this, slot (handlematherror ()));

Connect (calculator, signal (divisionbyzero (), this, slot (handlematherror (); that is to say, as long as any signal is sent, this slot will be called. 3. One signal can be connected to another signal:Connect (lineedit, signal (textchanged (const qstring &), this, signal (UpdateRecord (const qstring &); that is, 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. 4. The slot can be canceled:Disconnect (LCD, signal (overflow (), this, slot (handlematherror (); this situation does not often occur because after an object is deleted, qt automatically removes all slots connected to this object. 5. In order 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 &))); 6. 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. Note 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;

}; The following code is provided during use: 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 from: http://devbean.blog.51cto.com/448512/199461

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.