The meta-Object System (qt5)

Source: Internet
Author: User

> Qt's meta-object system provides the signals-slots mechanism, which can communicate with objects and support runtime information and dynamic attribute dynamic properties systems;

Based on three aspects
1) qobject class-provides a base class for all objects that can use meta-Object System;
2) The q_object macro is placed in the private part of the class declaration so that the class can use the features of meta-object, such as dynamic attributes and signals-slots;
3) MOC (meta-object compiler) generates necessary code for each qobject sub-object to implement the meta-object feature;

>MoCThe tool reads the C ++ source file. if a class containing the q_object macro is found, it creates another c ++ source file containing the meta-object code. The newly generated source file must be # inlcude to the class source file, or compile and link with the class implementation;

> In addition to providing the signals-slots mechanism for communication between objects, meta-object code also provides the following features:

-Qobject: metaobject () returns the meta-object bound to the class;
-Qmetaobject: Class Name of the string type returned by classname () at runtime. It does not need to be supported by the native runtime type information rtti of the c ++ compiler;
-Qobject: inherits () if the object is an instance of a class on the qobject inheritance tree, true is returned;
-Qobject: TR () and qobject: trutf8 () provide the International Translation function of strings;
-Qobject: setproperty () and qobject: Property () dynamically set and obtain attributes through object names;
-Qmetaobject: newinstance () to construct a new instance of this class;

> UseQobject_cast() Dynamically convert the type of the qobject class. The qobject_cast () function is similar to the Standard C ++ dynamic_cast function. The advantage is that it does not require rtti support and can span the boundaries of the dynamic library; it will try to cast the parameter as the pointer type in angle brackets. If the object type is correct (determined during runtime), a non-0 pointer will be returned, and 0 will be returned if the type does not match;

Example

// Assume that mywidget inherits from qwidget and declares the q_object macro qobject * OBJ = new mywidget; // The OBJ variable is a pointer of the qobject type and actually points to the mywidget object. Therefore, the conversion can be as follows: // The conversion from qobject to qwidget is successful, because the object points to the mywidget type and is a subclass of qwidget; qwidget * widget = qobject_cast <qwidget *> (OBJ ); // OBJ points to mywidget, so the conversion can also be converted to mywidget; // the conversion from mywiet to qobject_cast () is successful, because qobject_cast () has no difference between the QT built-in type and the custom type; mywidget * mywidget = qobject_cast <mywidget *> (OBJ); // The conversion to qlable fails, and the pointer is set to 0; qlabel * label = qobject _ Cast <qlabel *> (OBJ); // you can set 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 object can be used as the base classQ_objectMacro and meta-obejct code, but in this case, signals-slots and other features cannot be used. From the perspective of the meta-Object System, A qobject subclass without meta-object code is the same as its latest base class with meta-object code. For example, qmetaobject: classname () the exact name of the class is not returned, but the name of the base class is returned;

> Therefore, we strongly recommend that all qobject subclasses use the q_object macro, whether or not signals-slots and attributes;

<Refer to> http://qt-project.org/doc/qt-5.0/qtcore/metaobjects.html

End

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.