Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 1786

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qt-4.3/doc/src/metaobjects.qdoc -->
<head>
  <title>Meta-Object System</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Meta-Object System<br /><small></small></h1>
<p>The meta-object system is based on three things:</p>
<ol type="1">
<li>The <a href="core/QObject.html"><tt>QObject</tt></a> class provides a base class for objects that can take advantage of the meta-object system.</li>
<li>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.</li>
<li>The <a href="moc.html#moc">Meta-Object Compiler</tt></a> (<tt>moc</tt>) supplies each <a href="core/QObject.html"><tt>QObject</tt></a> subclass with the necessary code to implement meta-object features.</li>
</ol>
<p>The <tt>moc</tt> 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 <tt>#include</tt>'d into the class's source file or, more usually, compiled and linked with the class's implementation.</p>
<p>In addition to providing the <a href="qtjambi-signalsandslots.html">signals and slots</tt></a> mechanism for communication between objects (the main reason for introducing the system), the meta-object code provides the following additional features:</p>
<ul>
<li>QObject::metaObject() returns the associated meta-object</tt> for the class.</li>
<li>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.</li>
<li>QObject::inherits() function returns whether an object is an instance of a class that inherits a specified class within the <a href="core/QObject.html"><tt>QObject</tt></a> inheritance tree.</li>
<li>QObject::tr() and QObject::trUtf8() translate strings for internationalization</tt>.</li>
<li>QObject::setProperty() and QObject::property() dynamically set and get properties by name.</li>
</ul>
<a name="qobjectcast"></a><p>It is also possible to perform dynamic casts using qobject_cast() on <a href="core/QObject.html"><tt>QObject</tt></a> classes. The 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.</p>
<p>For example, let's assume <tt>MyWidget</tt> inherits from <a href="gui/QWidget.html"><tt>QWidget</tt></a> and is declared with the Q_OBJECT macro:</p>
<pre>        QObject *obj = new MyWidget;</pre>
<p>The <tt>obj</tt> variable, of type <tt>QObject *</tt>, actually refers to a <tt>MyWidget</tt> object, so we can cast it appropriately:</p>
<pre>        QWidget *widget = qobject_cast&lt;QWidget *&gt;(obj);</pre>
<p>The cast from <a href="core/QObject.html"><tt>QObject</tt></a> to <a href="gui/QWidget.html"><tt>QWidget</tt></a> is successful, because the object is actually a <tt>MyWidget</tt>, which is a subclass of <a href="gui/QWidget.html"><tt>QWidget</tt></a>. Since we know that <tt>obj</tt> is a <tt>MyWidget</tt>, we can also cast it to <tt>MyWidget *</tt>:</p>
<pre>        MyWidget *myWidget = qobject_cast&lt;MyWidget *&gt;(obj);</pre>
<p>The cast to <tt>MyWidget</tt> is successful because qobject_cast() makes no distinction between built-in Qt types and custom types.</p>
<pre>        QLabel *label = qobject_cast&lt;QLabel *&gt;(obj);
        <span class="comment">// label is 0</span></pre>
<p>The cast to <a href="gui/QLabel.html"><tt>QLabel</tt></a>, 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:</p>
<pre>        if (QLabel *label = qobject_cast&lt;QLabel *&gt;(obj)) {
            label-&gt;setText(tr(&quot;Ping&quot;));
        } else if (QPushButton *button = qobject_cast&lt;QPushButton *&gt;(obj)) {
            button-&gt;setText(tr(&quot;Pong!&quot;));
        }</pre>
<p>While it is possible to use <a href="core/QObject.html"><tt>QObject</tt></a> 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 <a href="core/QObject.html"><tt>QObject</tt></a> 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.</p>
<p>Therefore, we strongly recommend that all subclasses of <a href="core/QObject.html"><tt>QObject</tt></a> use the Q_OBJECT macro regardless of whether or not they actually use signals, slots, and properties.</p>
<dl>
<dt><b>See Also:</b></dt>
<dd><tt>QMetaObject</tt>, <a href="properties.html">Qt's Property System</tt></a>, <a href="qtjambi-signalsandslots.html">Signals and Slots</tt></a></dd>
</dl>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>