Principles and usage of QT's signal/Slot Mechanism

Source: Internet
Author: User

People who have used QT know that signal/slot is an efficient communication interface between QT objects and their derived objects. It is the core feature of QT, it is also important to distinguish it from other tool kits. It is completely independent from the Standard C/C ++ language. Therefore, to properly process signals and slots, you must use a QT tool to become a MOC (meta object compiler, this tool is a C ++ preprocessingProgramTo automatically generate high-level events.Code.

 

1. Different from the callback function:

 

The callback function passes a function pointer, which can easily cause program crash. On the other hand, the callback method is closely bound to the functional elements of the graphic user interface, so it is difficult to develop independent classification. The signal/slot mechanism can carry any number of parameters and does not generate core dumps as the callback function does.

 

In addition, you can connect n signals to a single slot, or connect n slots to a single signal, or even one signal to another. In this way, when a signal is sent, the signals or slots connected to it will be executed in a certain order (there is no predetermined order, that is, the execution order is random, emit returns only after all signals and slots connected to it are returned.

 

2. Signal definition:

 

Siganls:

Void mysignal ();

Void mysignal (int x );

Void mysignal (int x, int y );

Signals is the key word of QT, rather than the key word of C/C ++. In addition, the difference between a signal and a general function is that all of its return values are void, and it does not have a function implementation body, and its function body is automatically generated by MOC.

 

3. slot definition:

Public slots:

Void myslot ();

Void myslot (int x );

Different types of slots have different operation permissions, depending on whether the slot is public, protected, or private.

 

4. Signal and signal or connection to the slot:

Qobeject: connect (obj1, signal (mysignal (), obj2, slot (myslot ()));

Qobeject: connect (obj1, signal (mysignal (), obj2, signal (mysignal2 ()));

 

5. Signal and slot disconnection:

Qobeject: disconnect (obj1, signal (mysignal (), obj2, slot (myslot ()));

Qobeject: disconnect (obj1, signal (mysignal (), obj2, signal (mysignal2 ()));

 

This mechanism is very convenient for the operation of GUI controls. Of course, it should be appropriate, standardized, and scientific.

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.