The signal slot mechanism of QT development

Source: Internet
Author: User

First, the signal slot mechanism principle

1. How to declare a signal slot

A simplified version of the QT header file:

Class Example:public Qobject {q_objectsignals:void customsignal (); void customsignal (int i) public slots:void CustomSlot (); void Customslot (int i);};

2. Macro and MoC Source object

Excerpt code:

Qobjectdefs.hi. ... Ii. #define SLOTSIII. #define SIGNALS PUBLICIV. Emitvi. V. #define. ...

Why do we need MOC?

Since C + + native does not provide introspection, and QT's signal slots and attribute lists are based on introspection, it is easy to list the methods and attributes of the object by introspection, and it can be said that the signal slot of QT is the type safety.

For more information, refer to: interpreting the QT introspection mechanism (so-called introspection refers to the ability of object-oriented language to query object information during runtime)

Second, the development and use of signal slots

Qt5 before:

Connect (sender, SIGNAL (customsignal (int i)), receiver, SLOT (customslot (int i)));

Disadvantage: There is no compiler check because the signal and slot functions are processed into strings, the compiler cannot check for errors at compile time, and all checks are done at run time. This may occur if the compilation passes through the slot but is not called.

You can only view the error message in the console.

QT5 New wording:

Connect (sender, &sender::customsignal,receiver, &receiver::customslot);

Like the previous syntax, the new signal slot features:

    • The new syntax
    • Compiler check
    • Friendly Error Hints
    • Automatic parameter type conversion
    • Allows any function to be connected (using a function pointer to call the function directly, so the slot is not processed by the MOC.) The signal still needs to be processed by the MOC, which needs to be declared in a specific area

    • C++lambda expression (can be willful written in the following format)
Connect (this, &example::customsignal,[=] (int i) {cout << "resule:" << i;}

Common signal Slot usage:

Qtdesigner Create void example::on_pushbutton_clicked () {...} Connect (sender, SIGNAL (customsignal (int i)), receiver, SLOT (customslot (int i))); Connect (sender, &sender:: Customsignal,receiver, &receiver::customslot); Connect (sender, &sender::customsignal,custonslot); Connect (this, &example::customsignal,[=] (int i) {cout << "resule:" << i;}

Third, QT components and signal connection

Simple adder:

The UI interface is designed to

Right click pushbutton Select Go to slot Select clicked Method, will automatically generate and edit code in Summator, run

The signal slot mechanism of QT development

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.