The Meta-object System

Source: Internet
Author: User

The Meta-object System

The QT meta-object system provides signal and slot mechanisms, runtime information, and dynamic property systems for interaction between objects.

The meta-object system is based on three things:

1. The Qobject class acts as the base class for all the meta-object systems to be exploited.

2. Declare the Q_object macro in the private segment of the class so that you can use meta-object attributes, such as dynamic properties, signals and slots.

3. The meta-object compiler provides the necessary code for the Qobject subclass to implement the meta-object attributes.

The MOC Tool reads C + + source files. If one or more of the class declarations are found to contain the Q_object macro. It generates an additional C + + source file containing the meta-object code for each class. These generated source files are not only included in the source file of the class, but are used in both compilation and linking.

In addition to provide a signal and slot mechanism for communication between objects, the meta-object code provides the following features:

1. Qobject::metaobject () returns the Meta object associated with the class.

2. Qmetaobject::classname () returns the runtime class name, which is not required with the C + + compiler's Rtti.

3. Qobject::inherits () returns whether an object is an instance of a class in the Qobject inheritance tree.

4. QOBJECT::TR () and Qobject::trutf8 () convert a string for internationalization.

5. Qobject::setproperty () and Qobject::p Roperty () dynamically set and get properties by name.

6. Qmetaobject::newinstance () Creates an instance of the class.

It is also possible to use dynamic type conversion qobject_cast () on Qobject. Qobject_cast () is similar to the C + + dynamic_cast (), which has the advantage of not requiring C + + RTTI support and the ability to cross dynamic library boundaries. It attempts to convert the argument to the pointer type specified in angle brackets, and returns a pointer other than 0 if the object is of the correct type, otherwise 0.

For example: We assume that Mywidget inherits the Qwidget, and the Q_object macros are declared in the class.

qobject*obj =newmywidget;

The obj variable, which is a pointer to the Qobject type. Point to a mywidget object, so we can convert:

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

The transition from Qobject to Qwidget is successful because the object is essentially a mywidget, which is a subclass of Qwidget. So we know that obj is a mywidget, and we can convert it to Mywidget *.

Mywidget *mywidget = qobject_cast<mywidget *> (obj);

Conversions can be successful because qobject_cast () does not differentiate between QT built-in types and custom types.

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

Converting to Qlabel is a failure. The pointer is set to 0. This allows us to handle different types of object runtimes, depending on the type.

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

You can also use Qobject as a base class but do not include q_object macros and meta-object code. If you don't use Q_object macros, the signals and slots and the other features described here are not available. From the meta-object system's point of view, the Qobject subclass that does not contain meta-code is equivalent to the meta-object code of its nearest ancestor class. For example, Qmetaobject::classname () will not return your actual class name, but rather the name of the class's ancestor class.

Therefore, we strongly recommend that you use Q_object macros in all Qobject subclasses, regardless of the signal-to-slot mechanism and attributes.

http://blog.csdn.net/hai200501019/article/details/9155987

The 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.