Inside QT series (9): qmetaobject class data members

Source: Internet
Author: User

Let's take a look at the definition of qmetaobject. Let's take a look at the member data contained in the qmetaobject object first.

Struct q_core_export qmetaobject
{
//......
Struct {// Private Data
Const qmetaobject * superdata;
Const char * stringdata;
Const uint * data;
Const void * extradata;
} D;
};

The above code is all data members defined by the qmetaobject class. These members record so much information about signal, slot, property, and class. Let's explain these member variables one by one:

Const qmetaobject * superdata:
This variable points to the parent class of the corresponding qobject class, or the qmetaobject object of the ancestor class.

How can we understand this sentence? We know that each qmetaobject object must have a corresponding qobject class (or a subclass derived directly or indirectly from it). Note: The class is not an object.

Therefore, each qobject class (or its derived class) may have a parent class, a parent class of the parent class, or many ancestor classes before the hierarchy. Or there is no parent class (qobject ). Therefore, the superdata variable points to the qmetaobject object in the ancestor class closest to it. For qmetaobject objects of the qobject class, this is a null pointer because qobject has no parent class.

Let's give an example:

Class animal: Public qobject
{
Q_object
//.............
};

Class Cat: Public animal
{
Q_object
//.............
}

So the cat: staticmetaobject. D. superdata pointer variable points to the object animal: staticmetaobject
The animal: staticmetaobject. D. superdata pointer variable points to the object qobject: staticmetaobject.
The value of qobject: staticmetaobject. D. superdat is null.

However, if we change the above class definition to the following definition, it will be different:

Class animal: Public qobject
{
// Q_object. This class does not define this
//.............
};

Class Cat: Public animal
{
Q_object
//.............
}

So the cat: staticmetaobject. D. superdata pointer variable points to the object qobject: staticmetaobject
Because the animal: staticmetaobject object does not exist.

Const char * stringdata:
As the name suggests, this is a pointer to string data. However, it is very different from the general string pointer we usually use. The string pointer we usually use is just a pointer to a string, but this pointer points to many strings. Isn't it a string array? Haha, no. Because the string array of C ++ requires that each string in the array has the same length, in order to form an array. Is it a string pointer array? No, so what is it? Let's take a look at its specific values. Let's take the qmetaobject of the qobject class as an example.

Qobject: staticmetaobject. d. the stringdata Pointer Points to multiple string arrays. In fact, it points to a continuous memory zone, which stores several strings.

Static const char qt_meta_stringdata_qobject [] =
{
"Qobject/0/0 destroyed (qobject *)/0 destroyed ()/0"
"Deletelater ()/0_q_reregistertimers (void *)/0"
"Qstring/0 objectname/0 parent/0 qobject (qobject *)/0"
"Qobject ()/0"
};

What is the content of this string? Yes, class name, signal name, slot name, property name. Are you familiar with these features? Yes, they are the core functional attributes supported by meta system.

Since they are all unequal strings, how does QT index these strings so that they can be found correctly when needed? The third member officially debuted.

Const uint * data;
In essence, this pointer points to an array of positive integers, but the length of the array varies in different objects, depending on the number of signal, slot, and property defined in the corresponding class.

Some of the values in this integer array indicate the index values of different strings in the previous variable (stringdata). However, note that, the value here does not directly indicate the index value of each string. This value must be calculated using a corresponding algorithm to obtain the index value of the correct string.

The following is the value of the positive integer array pointed to by the qobject: staticmetaobject. D. Data Pointer.

Static const uint qt_meta_data_qobject [] =
{
// Content:
2, // Revision
0, // classname
0, 0, // classinfo
4, 12, // Methods
1, 32, // Properties
0, 0, // enums/Sets
2, 35, // Constructors

// Signals: Signature, parameters, type, Tag, flags
9, 8, 8, 8, 0 × 05,
29, 8, 8, 8, 0 × 25,

// Slots: Signature, parameters, type, Tag, flags
41, 8, 8, 8, 0 × 0a,
55, 8, 8, 8, 0 × 08,

// Properties: name, type, flags
90, 82, 0 × 0a095103,

// Constructors: Signature, parameters, type, Tag, flags
108,101, 8, 8, 0 × 0e,
126, 8, 8, 8, 0 × 2E,

0 // EOD
};

Briefly,

The first section is the integer of the // content area. The number of Entity objects in each qmetaobject is the same and the meaning is the same, but the specific values are different. Specifically, a struct defines this section, and its meaning is clearly stated in the above annotations.

Struct qmetaobjectprivate
{
Int revision;
Int classname;
Int classinfocount, classinfodata;
Int methodcount, methoddata;
Int propertycount, propertydata;
Int enumeratorcount, enumeratordata;
Int constructorcount, constructordata;
};

This struct defines the first secton. It is clear to compare it with the preceding value, right?

The second section, which starts with // signals. The value in this section indicates that the qobject class contains two signal,

The third section, which starts with // slots. The value in this section indicates that the qobject class contains two slots.

Section 4, starting with // properties. The value in this section indicates that the class qobject contains an attribute definition.

The fifth section, which starts with // constructors, indicates that the qobject class has two constructors.

Const void * extradata;
This is a pointer to the qmetaobjectextradata data structure. For this pointer, I will skip it here.

It is a bit difficult to calculate the corresponding algorithm between each specific integer value and the object data it points to. I will not explain the details here. If you are interested, read the source code by yourself, there will certainly be many discoveries.

========================================================== ====================================
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/81

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.