<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- metaobjects.qdoc --> <title>Qt 4.8: The Meta-Object System</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li>The Meta-Object System</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <h1 class="title">The Meta-Object System</h1> <span class="subtitle"></span> <!-- $$$metaobjects.html-description --> <div class="descr"> <a name="details"></a> <a name="meta-object"></a><a name="meta-object-system"></a><p>Qt's meta-object system provides the signals and slots mechanism for inter-object communication, run-time type information, and the dynamic property system.</p> <p>The meta-object system is based on three things:</p> <ol class="1"> <li>The <a href="qobject.html">QObject</a> class provides a base class for objects that can take advantage of the meta-object system.</li> <li>The <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> 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</a> (<tt>moc</tt>) supplies each <a href="qobject.html">QObject</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 <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> 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="signalsandslots.html#signals-and-slots">signals and slots</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><a href="qobject.html#metaObject">QObject::metaObject</a>() returns the associated <a href="qmetaobject.html">meta-object</a> for the class.</li> <li><a href="qmetaobject.html#className">QMetaObject::className</a>() 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><a href="qobject.html#inherits">QObject::inherits</a>() function returns whether an object is an instance of a class that inherits a specified class within the <a href="qobject.html">QObject</a> inheritance tree.</li> <li><a href="qobject.html#tr">QObject::tr</a>() and <a href="qobject.html#trUtf8">QObject::trUtf8</a>() translate strings for <a href="internationalization.html">internationalization</a>.</li> <li><a href="qobject.html#setProperty">QObject::setProperty</a>() and <a href="qobject.html#property">QObject::property</a>() dynamically set and get properties by name.</li> <li><a href="qmetaobject.html#newInstance">QMetaObject::newInstance</a>() constructs a new instance of the class.</li> </ul> <a name="qobjectcast"></a><p>It is also possible to perform dynamic casts using <a href="qobject.html#qobject_cast">qobject_cast</a>() on <a href="qobject.html">QObject</a> classes. The <a href="qobject.html#qobject_cast">qobject_cast</a>() 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="qwidget.html">QWidget</a> and is declared with the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro:</p> <pre class="cpp"> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>obj <span class="operator">=</span> <span class="keyword">new</span> 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 class="cpp"> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span><span class="operator">></span>(obj);</pre> <p>The cast from <a href="qobject.html">QObject</a> to <a href="qwidget.html">QWidget</a> is successful, because the object is actually a <tt>MyWidget</tt>, which is a subclass of <a href="qwidget.html">QWidget</a>. Since we know that <tt>obj</tt> is a <tt>MyWidget</tt>, we can also cast it to <tt>MyWidget *</tt>:</p> <pre class="cpp"> MyWidget <span class="operator">*</span>myWidget <span class="operator">=</span> qobject_cast<span class="operator"><</span>MyWidget <span class="operator">*</span><span class="operator">></span>(obj);</pre> <p>The cast to <tt>MyWidget</tt> is successful because <a href="qobject.html#qobject_cast">qobject_cast</a>() makes no distinction between built-in Qt types and custom types.</p> <pre class="cpp"> <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span><span class="operator">></span>(obj); <span class="comment">// label is 0</span></pre> <p>The cast to <a href="qlabel.html">QLabel</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 class="cpp"> <span class="keyword">if</span> (<span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span><span class="operator">></span>(obj)) { label<span class="operator">-</span><span class="operator">></span>setText(tr(<span class="string">"Ping"</span>)); } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span><span class="operator">></span>(obj)) { button<span class="operator">-</span><span class="operator">></span>setText(tr(<span class="string">"Pong!"</span>)); }</pre> <p>While it is possible to use <a href="qobject.html">QObject</a> as a base class without the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro and without meta-object code, neither signals and slots nor the other features described here will be available if the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro is not used. From the meta-object system's point of view, a <a href="qobject.html">QObject</a> subclass without meta code is equivalent to its closest ancestor with meta-object code. This means for example, that <a href="qmetaobject.html#className">QMetaObject::className</a>() 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="qobject.html">QObject</a> use the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro regardless of whether or not they actually use signals, slots, and properties.</p> </div> <p><b>See also </b><a href="qmetaobject.html">QMetaObject</a>, <a href="properties.html#qt-s-property-system">Qt's Property System</a>, and <a href="signalsandslots.html#signals-and-slots">Signals and Slots</a>.</p> <!-- @@@metaobjects.html --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>