« | July 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | |
| 公告 |
☆★☆★☆★☆★☆★☆ 生活的点点记录,以及一些体会...........
喜欢是淡淡的爱,爱是深深的喜欢.
时间会见证一切.......................
欢迎大家指出错误,共同进步..........
期待中..............................
☆★☆★☆★☆★☆★☆ |
Blog信息 |
blog名称: 日志总数:162 评论数量:312 留言数量:0 访问次数:939671 建立时间:2005年5月17日 |

| |
[读书笔记]qt study 文章收藏
oceanblue 发表于 2008/12/19 15:42:23 |
Meta-Object System
Qt's Meta-Object System provides the signals and slots mechanism for
inter-object communication, run-time type information, and the dynamic
property system.
The Meta-Object System is based on three things:
The QObject class provides a base class for objects that can take advantage of the meta-object system.The Q_OBJECT
macro inside the private section of the class declaration is used to
enable meta-object features, such as dynamic properties, signals, and
slots.The Meta-Object Compiler (moc) supplies each QObject subclass with the necessary code to implement meta-object features.
The moc tool reads a C++ source file. If it finds one or more class declarations that contain the Q_OBJECT
macro, it produces another C++ source file which contains the
meta-object code for each of those classes. This generated source file
is either #include'd into the class's source file or, more usually, compiled and linked with the class's implementation.
In addition to providing the signals and slots
mechanism for communication between objects (the main reason for
introducing the system), the meta-object code provides the following
additional features:
QObject::metaObject() returns the associated meta-object for the class.QMetaObject::className()
returns the class name as a string at run-time, without requiring
native run-time type information (RTTI) support through the C++
compiler.QObject::inherits() function returns whether an object is an instance of a class that inherits a specified class within the QObject inheritance tree.QObject::tr() and QObject::trUtf8() translate strings for internationalization.QObject::setProperty() and QObject::property() dynamically set and get properties by name.
It is also possible to perform dynamic casts using qobject_cast() on QObject classes. The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(),
with the advantages that it doesn't require RTTI support and it works
across dynamic library boundaries. It attempts to cast its argument to
the pointer type specified in angle-brackets, returning a non-zero
pointer if the object is of the correct type (determined at run-time),
or 0 if the object's type is incompatible.
For example, let's assume MyWidget inherits from QWidget and is declared with the Q_OBJECT macro:
QObject *obj = new MyWidget;
The obj variable, of type QObject *, actually refers to a MyWidget object, so we can cast it appropriately:
QWidget *widget = qobject_cast<QWidget *>(obj);
The cast from QObject to QWidget is successful, because the object is actually a MyWidget, which is a subclass of QWidget. Since we know that obj is a MyWidget, we can also cast it to MyWidget *:
MyWidget *myWidget = qobject_cast<MyWidget *>(obj);
The cast to MyWidget is successful because qobject_cast() makes no distinction between built-in Qt types and custom types.
QLabel *label = qobject_cast<QLabel *>(obj); // label is 0
The cast to QLabel,
on the other hand, fails. The pointer is then set to 0. This makes it
possible to handle objects of different types differently at run-time,
based on the type:
if (QLabel *label = qobject_cast<QLabel *>(obj)) { label->setText(tr("Ping")); } else if (QPushButton *button = qobject_cast<QPushButton *>(obj)) { button->setText(tr("Pong!")); }
While it is possible to use QObject as a base class without the Q_OBJECT macro and without meta-object code, neither signals and slots nor the other features described here will be available if the Q_OBJECT macro is not used. From the meta-object system's point of view, a QObject subclass without meta code is equivalent to its closest ancestor with meta-object code. This means for example, that QMetaObject::className() will not return the actual name of your class, but the class name of this ancestor.
Therefore, we strongly recommend that all subclasses of QObject use the Q_OBJECT macro regardless of whether or not they actually use signals, slots, and properties. |
|
|