QT Learning Pathway (9): deep understanding of signal slots

Source: Internet
Author: User
Tags ftp connect

There is no big difference between a slot function and a normal C + + member function. They can also be virtual, can be overridden, can be public, protected, or private, can be called by other C + + functions, and arguments can be of any type. If you want to say the difference, that is, the slot function can be connected to a signal, when this signal occurs, it can be automatically invoked.

The Connect () statement has a prototype similar to the following:

Connect (sender, SIGNAL (SIGNAL), receiver, SLOT (SLOT));

Here, sender and receiver are qobject types, Singal and slot are function signatures that have no parameter names. Singal () and slot () macros are used to convert arguments to strings.

In depth, there are more possible uses for the signal slots, as shown below.

A signal can be connected with multiple slots:

Connect (slider, SIGNAL (valuechanged (int)),
Spinbox, SLOT (setValue (int)));
Connect (slider, SIGNAL (valuechanged (int)),
This, SLOT (updatestatusbarindicator (int)));

Note that if this is the case, these slots will be called one after another, but the order of their invocation is indeterminate.

Multiple signals can be connected to a slot:

Connect (LCD, SIGNAL (overflow ()),
This, SLOT (Handlematherror ()));
Connect (Calculator, SIGNAL (Divisionbyzero ()),
This, SLOT (Handlematherror ()));

This means that whenever any one of the signals is emitted, the slot is invoked.

A signal can be connected to another signal:

Connect (Lineedit, SIGNAL (textchanged (const QString &)),
This, SIGNAL (UpdateRecord (const QString &)));

This is to say that when the first signal is emitted, the second signal is emitted. In addition, this signal-signal form and signal-groove form is no different.

Slots can be unlinked:

Disconnect (LCD, SIGNAL (overflow ()),
This, SLOT (Handlematherror ()));

This is not often the case, because QT automatically cancels all the slots connected to the object after the object deletes it.

For the proper connection of the signal slot, the number, type, and order 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 &)));

An exception to this is that if the signal has more parameters than the slot, then those parameters after this parameter are ignored, for example:

Connect (FTP, SIGNAL (rawcommandreply (int, const QString &)),
This, SLOT (checkerrorcode (int)));

Here, the const QString & This parameter is ignored by the slot.

Related Article

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.