path: root/src/corelib/doc/src/objectmodel/metaobjects.qdoc
diff options
authorCasper van Donderen <>2012-03-20 19:37:07 +0100
committerQt by Nokia <>2012-04-19 07:34:53 +0200
commit0bc02fd0d61d1e4aed9b39890d28975dff30e822 (patch)
treee967ab719c7f8df24c35b088bd48e0f5b0942148 /src/corelib/doc/src/objectmodel/metaobjects.qdoc
parent7f0c130be963de90d1baeb037820b17a4f298700 (diff)
Doc: Prepare for building modular QtCore docs.
This change fixes most qdoc errors in QtCore. There are about 900 left. The main thing this change does is moving documentation from qtcore from /doc/src to /src/corelib/doc. Other issues resolved are mis-use of qdoc commands. Change-Id: I002d01edfb13575e8bf27ce91596a577a92562d1 Reviewed-by: Lars Knoll <> Reviewed-by: Jason McDonald <> Reviewed-by: Jerome Pasion <>
Diffstat (limited to 'src/corelib/doc/src/objectmodel/metaobjects.qdoc')
1 files changed, 137 insertions, 0 deletions
diff --git a/src/corelib/doc/src/objectmodel/metaobjects.qdoc b/src/corelib/doc/src/objectmodel/metaobjects.qdoc
new file mode 100644
index 0000000000..8e071be91e
--- /dev/null
+++ b/src/corelib/doc/src/objectmodel/metaobjects.qdoc
@@ -0,0 +1,137 @@
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact:
+** This file is part of the documentation of the Qt Toolkit.
+** GNU Free Documentation License
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file.
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms
+** and conditions contained in a signed written agreement between you
+** and Nokia.
+ \page metaobjects.html
+ \title The Meta-Object System
+ \brief An overview of Qt's meta-object system and introspection capabilities.
+ \ingroup qt-basic-concepts
+ \keyword meta-object
+ \target 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:
+ \list 1
+ \li The \l QObject class provides a base class for objects that can
+ take advantage of the meta-object system.
+ \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 The \l{moc}{Meta-Object Compiler} (\c moc) supplies each
+ QObject subclass with the necessary code to implement
+ meta-object features.
+ \endlist
+ The \c 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 \c{#include}'d into the class's source file or, more
+ usually, compiled and linked with the class's implementation.
+ In addition to providing the \l{signals and slots} mechanism for
+ communication between objects (the main reason for introducing
+ the system), the meta-object code provides the following
+ additional features:
+ \list
+ \li QObject::metaObject() returns the associated
+ \l{QMetaObject}{meta-object} for the class.
+ \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 QObject::inherits() function returns whether an object is an
+ instance of a class that inherits a specified class within the
+ QObject inheritance tree.
+ \li QObject::tr() and QObject::trUtf8() translate strings for
+ \l{Internationalization with Qt}{internationalization}.
+ \li QObject::setProperty() and QObject::property()
+ dynamically set and get properties by name.
+ \li QMetaObject::newInstance() constructs a new instance of the class.
+ \endlist
+ \target qobjectcast
+ It is also possible to perform dynamic casts using qobject_cast()
+ on QObject classes. The qobject_cast() function behaves similarly
+ to the standard C++ \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 \c MyWidget inherits from QWidget and
+ is declared with the Q_OBJECT macro:
+ \snippet qtcast/qtcast.cpp 0
+ The \c obj variable, of type \c{QObject *}, actually refers to a
+ \c MyWidget object, so we can cast it appropriately:
+ \snippet qtcast/qtcast.cpp 1
+ The cast from QObject to QWidget is successful, because the
+ object is actually a \c MyWidget, which is a subclass of QWidget.
+ Since we know that \c obj is a \c MyWidget, we can also cast it to
+ \c{MyWidget *}:
+ \snippet qtcast/qtcast.cpp 2
+ The cast to \c MyWidget is successful because qobject_cast()
+ makes no distinction between built-in Qt types and custom types.
+ \snippet qtcast/qtcast.cpp 3
+ \snippet qtcast/qtcast.cpp 4
+ 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:
+ \snippet qtcast/qtcast.cpp 5
+ \snippet qtcast/qtcast.cpp 6
+ 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.
+ \sa QMetaObject, {Qt's Property System}, {Signals and Slots}