Qt Meta-object Systems (Meta-object system)

Source: Internet
Author: User

(Transferred from: http://blog.csdn.net/aladdina/article/details/5496891)

The QT meta-object system is based on the following three things:

    1. Class: Qobject provides a base class for all objects that need to take advantage of the original object system.
    2. Macros: Q_object, which can typically be declared in a private segment of a class, allows the class to use the attributes of a meta-object object, such as dynamic properties, signals, and slots.
    3. Compiler: The Meta Object Compiler (MOC) automatically generates the necessary code for each Qobject sub-object to implement the meta-object attributes.

The MOC tool reads the source file for C + +, and if it discovers one or more classes that declare the Q_object macro, it creates another C + + source file, generating code for each class that contains the meta-object implementation. The source files generated by these compilations are usually included in the source file of the class or are compiled and linked at the same time as the implementation of the class.

In addition to providing a signal and slot (signals and slots) mechanism for communication between objects, the code of the meta-object also provides the following features:

    • Qobject::metaobject () returns the Meta-object object that is bound to the class.
    • Qmetaobject::classname () can return the name of the class as a string at run time, without the support of the C + + compiler native run-time type information (RTTI).
    • The Qobject::inherits () function returns the inheritance information: whether the object is an instance of a class on the Qobject inheritance tree.
    • Qobject::tr () and Qobject::trutf8 () provide internationalization support for translating strings into the specified language.
    • Qobject::setproperty () and Qobject::p Roperty () dynamically set and get object properties by name.
    • Qmetaobject::newinstance () Constructs a new instance of the class.

In addition, you can dynamically convert the type of the Qobject class with Qobject_cast (). The Qobject_cast () function is similar to the dynamic_cast () feature of standard C + +, except that it does not require RTTI support and can span the boundaries of a dynamically connected library. It attempts to cast its arguments into an object type within angle brackets, and returns nonzero if the object is of the correct type (run-time decision), otherwise returns 0, which indicates that the object type is incompatible.

For example, suppose Mywidget inherits from Qwidget and also declares a Q_object macro,

Qobject *obj = new Mywidget;

The variable obj of the Qobject type actually points to a Mywidget object, so we can do this type conversion:

Qwidget *widget = qobject_cast<qwidget *> (obj);

The transition to Mywidget can be successful because qobject_cast () does not treat QT built-in objects and custom extension objects separately.

Qlabel *label = Qobject_cast<qlabel *> (obj);
Label is 0

On the other hand, the transition to Qlabel will fail and the pointer will be set to 0. This allows us to handle different types of objects differently at run time, depending on the object type:

if (Qlabel *label = Qobject_cast<qlabel *> (obj)) {
Label->settext (tr ("Ping"));
} else if (Qpushbutton *button = Qobject_cast<qpushbutton *> (obj)) {
Button->settext (tr ("pong!"));
}

Although we can still use Qobject as the base class without q_object the macros and the original object information, it is not possible to use the features such as signals and slots and other attributes described here. From the meta-object system's point of view, a qobject subclass with no meta-object code is equivalent to its closest ancestor with the meta-object code. This means that qmetaobject::classname () will not return the real name of your class, but rather the name of one of the ancestors of that class.

Therefore, we strongly recommend that all Qobject subclasses be used with Q_object macros, regardless of whether you actually use signals and slots, as well as attributes.

See Qmetaobject, Qt ' s property System, signals and Slots.

Original link: http://doc.trolltech.com/4.6/metaobjects.html

Welcome reprint, please specify Source: Http://blog.csdn.net/aladdina

Qt Meta-object Systems (Meta-object system)

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.