Inside QT series (10): connect, the story behind the scenes

Source: Internet
Author: User

We all know that to connect a signal to a slot, we need to use the Connect Method of the qobject class. Its function is to connect the signal of an object to the slot of another object, to achieve communication between objects.

What have connect done behind the scenes? Why is the corresponding slot called after emit has a signal? Well, let's solve the mysteries one by one.

Signal and slot macro definitions

When we call the connect method, we usually write as follows:
OBJ. Connect (& OBJ, signal (destroyed (), & App, slot (aboutqt ()));
Here we can see that the names of signal and slot are included in two upper-case signal and slot. What are the two names? Originally, signal and slot were two macros defined by QT. Well, let's take a look at what these two macros have written:

Here are the definitions of these two macros:
# Define slot (a) "1" #
# Define signal (a) "2" #

Originally, QT converted signal and slot into strings, and added additional symbols before the string. '2' was added before signal, and '1' was added before slot '. That is to say, we have previously written the following Connect call. After MOC compiler conversion, it will become:
OBJ. Connect (& OBJ, "2 destroyed ()", & App, "1 aboutqt ()"));

After the connect function is called, it will check whether the two parameters are converted using the two macros. It checks the two front numbers, whether it is equal to 1 or 2. If not, the connect function will certainly fail!

Then, the system checks whether the signal object sent has this signal. The method is to find the D contained in the staticmetaobject object corresponding to the class of this object. whether the string pointed to by stringdata contains the signal name will be used during this check. the integer that data points to. These integers are used to calculate the starting address of each specific string. Similarly, the same method will be used to check the slot to see if the object responding to this signal contains the corresponding slot. If either of the two checks fails, the connect function fails and false is returned.

The previous steps are all necessary checks. The next step is to associate the object that sends signal with the object that responds to signal. In qobjectprivate, The qobject private data class has the following data structure to save the information:

Class qobjectprivate: Public qobjectdata
{
Struct connection
{
Qobject * Cycler;
Int method;
Uint connectiontype: 3; // 0 = auto, 1 = direct, 2 = queued, 4 = Blocking
Qbasicatomicpointer <int> argumenttypes;
};

Typedef qlist <connection> connectionlist;

Qobjectconnectionlistvector * connectionlists;

Struct sender
{
Qobject * sender;
Int signal;
Int ref;
};

Qlist <sender> senders;
}

In the signal sending object, each signal and slot connection creates a qobjectprivate: connection object, and saves the object to the connectionlist vector.

In the signal response object, every signal and slot connection also creates a sender object and attaches this object to the senders list.

The above is the connect process. The process of creating the qobjectprivate: connection object and sender object is a little complicated. You need to think carefully before you can read the source code.

========================================================== ====================================
Statement:
Inside QT series is an original technical article (http://www.qkevin.com.
This series of columns can be reproduced at will, but the statement and original address of each article must be kept.
The Copyright reserved by the author shall not be used for any commercial purposes without the consent of the author

Total index of the column in inside QT series: http://www.qkevin.com/qt
Original address: http://www.qkevin.com/archives/85

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.