http://www.wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/metaobjects&feed=atom&action=historyQt:Документация 4.3.2/metaobjects - История изменений2024-03-29T10:55:04ZИстория изменений этой страницы в викиMediaWiki 1.15.1http://www.wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/metaobjects&diff=4966&oldid=prevRoot: Новая: {{Qt4.3.2_header}} =Meta-Object System<br />= The meta-object system is based on three things: #The QObject class provides a base class...2009-01-29T10:28:23Z<p>Новая: {{Qt4.3.2_header}} =Meta-Object System<br />= The meta-object system is based on three things: #The <a href="/index.php/Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/qobject" title="Qt:Документация 4.3.2/qobject"> QObject</a> class provides a base class...</p>
<p><b>Новая страница</b></p><div>{{Qt4.3.2_header}}<br />
<br />
=Meta-Object System<br />=<br />
<br />
The meta-object system is based on three things:<br />
<br />
#The [[Qt:Документация 4.3.2/qobject | QObject]] class provides a base class for objects that can take advantage of the meta-object system.<br />
#The [[Qt:Документация 4.3.2/qobject#Q_OBJECT | 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.<br />
#The [[Qt:Документация 4.3.2/moc#moc | Meta-Object Compiler]] (<tt>moc</tt>) supplies each [[Qt:Документация 4.3.2/qobject | QObject]] subclass with the necessary code to implement meta-object features.<br />
<br />
The <tt>moc</tt> tool reads a C++ source file. If it finds one or more class declarations that contain the [[Qt:Документация 4.3.2/qobject#Q_OBJECT | 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 <tt>#include</tt>'d into the class's source file or, more usually, compiled and linked with the class's implementation.<br />
<br />
In addition to providing the [[Qt:Документация 4.3.2/signalsandslots | signals and slots]] mechanism for communication between objects (the main reason for introducing the system), the meta-object code provides the following additional features:<br />
<br />
*[[Qt:Документация 4.3.2/qobject#metaObject | QObject::metaObject]]() returns the associated [[Qt:Документация 4.3.2/qmetaobject | meta-object]] for the class.<br />
*[[Qt:Документация 4.3.2/qmetaobject#className | 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.<br />
*[[Qt:Документация 4.3.2/qobject#inherits | QObject::inherits]]() function returns whether an object is an instance of a class that inherits a specified class within the [[Qt:Документация 4.3.2/qobject | QObject]] inheritance tree.<br />
*[[Qt:Документация 4.3.2/qobject#tr | QObject::tr]]() and [[Qt:Документация 4.3.2/qobject#trUtf8 | QObject::trUtf8]]() translate strings for [[Qt:Документация 4.3.2/i18n | internationalization]].<br />
*[[Qt:Документация 4.3.2/qobject#setProperty | QObject::setProperty]]() and [[Qt:Документация 4.3.2/qobject#property | QObject::property]]() dynamically set and get properties by name.<br />
<div id="qobjectcast"></div><br />
It is also possible to perform dynamic casts using [[Qt:Документация 4.3.2/qobject#qobject_cast | qobject_cast]]() on [[Qt:Документация 4.3.2/qobject | QObject]] classes. The [[Qt:Документация 4.3.2/qobject#qobject_cast | qobject_cast]]() function behaves similarly to the standard C++ <tt>dynamic_cast()</tt>, 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.<br />
<br />
For example, let's assume <tt>MyWidget</tt> inherits from [[Qt:Документация 4.3.2/qwidget | QWidget]] and is declared with the [[Qt:Документация 4.3.2/qobject#Q_OBJECT | Q_OBJECT]] macro:<br />
<source lang="cpp-qt"> QObject *obj = new MyWidget;</source> <br />
The <tt>obj</tt> variable, of type <tt>QObject *</tt>, actually refers to a <tt>MyWidget</tt> object, so we can cast it appropriately:<br />
<source lang="cpp-qt"> QWidget *widget = qobject_cast<QWidget *>(obj);</source> <br />
The cast from [[Qt:Документация 4.3.2/qobject | QObject]] to [[Qt:Документация 4.3.2/qwidget | QWidget]] is successful, because the object is actually a <tt>MyWidget</tt>, which is a subclass of [[Qt:Документация 4.3.2/qwidget | QWidget]]. Since we know that <tt>obj</tt> is a <tt>MyWidget</tt>, we can also cast it to <tt>MyWidget *</tt>:<br />
<source lang="cpp-qt"> MyWidget *myWidget = qobject_cast<MyWidget *>(obj);</source> <br />
The cast to <tt>MyWidget</tt> is successful because [[Qt:Документация 4.3.2/qobject#qobject_cast | qobject_cast]]() makes no distinction between built-in Qt types and custom types.<br />
<source lang="cpp-qt"> QLabel *label = qobject_cast<QLabel *>(obj);<br />
// label is 0</source> <br />
The cast to [[Qt:Документация 4.3.2/qlabel | 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:<br />
<source lang="cpp-qt"> if (QLabel *label = qobject_cast<QLabel *>(obj)) {<br />
label->setText(tr("Ping"));<br />
} else if (QPushButton *button = qobject_cast<QPushButton *>(obj)) {<br />
button->setText(tr("Pong!"));<br />
}</source> <br />
While it is possible to use [[Qt:Документация 4.3.2/qobject | QObject]] as a base class without the [[Qt:Документация 4.3.2/qobject#Q_OBJECT | Q_OBJECT]] macro and without meta-object code, neither signals and slots nor the other features described here will be available if the [[Qt:Документация 4.3.2/qobject#Q_OBJECT | Q_OBJECT]] macro is not used. From the meta-object system's point of view, a [[Qt:Документация 4.3.2/qobject | QObject]] subclass without meta code is equivalent to its closest ancestor with meta-object code. This means for example, that [[Qt:Документация 4.3.2/qmetaobject#className | QMetaObject::className]]() will not return the actual name of your class, but the class name of this ancestor.<br />
<br />
Therefore, we strongly recommend that all subclasses of [[Qt:Документация 4.3.2/qobject | QObject]] use the [[Qt:Документация 4.3.2/qobject#Q_OBJECT | Q_OBJECT]] macro regardless of whether or not they actually use signals, slots, and properties.<br />
<br />
See also [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]], [[Qt:Документация 4.3.2/properties | Qt's Property System]], and [[Qt:Документация 4.3.2/signalsandslots | Signals and Slots]].<br />
<br />
<br />
{{Qt4.3.2_footer}}</div>Root