| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \page metaobjects.html
|
|---|
| 44 | \title Meta-Object System
|
|---|
| 45 | \brief An overview of Qt's meta-object system and introspection capabilities.
|
|---|
| 46 | \ingroup architecture
|
|---|
| 47 | \keyword meta-object
|
|---|
| 48 |
|
|---|
| 49 | Qt's meta-object system provides the signals and slots mechanism for
|
|---|
| 50 | inter-object communication, run-time type information, and the dynamic
|
|---|
| 51 | property system.
|
|---|
| 52 |
|
|---|
| 53 | The meta-object system is based on three things:
|
|---|
| 54 |
|
|---|
| 55 | \list 1
|
|---|
| 56 | \o The \l QObject class provides a base class for objects that can
|
|---|
| 57 | take advantage of the meta-object system.
|
|---|
| 58 | \o The Q_OBJECT macro inside the private section of the class
|
|---|
| 59 | declaration is used to enable meta-object features, such as
|
|---|
| 60 | dynamic properties, signals, and slots.
|
|---|
| 61 | \o The \l{moc}{Meta-Object Compiler} (\c moc) supplies each
|
|---|
| 62 | QObject subclass with the necessary code to implement
|
|---|
| 63 | meta-object features.
|
|---|
| 64 | \endlist
|
|---|
| 65 |
|
|---|
| 66 | The \c moc tool reads a C++ source file. If it finds one or more
|
|---|
| 67 | class declarations that contain the Q_OBJECT macro, it
|
|---|
| 68 | produces another C++ source file which contains the meta-object
|
|---|
| 69 | code for each of those classes. This generated source file is
|
|---|
| 70 | either \c{#include}'d into the class's source file or, more
|
|---|
| 71 | usually, compiled and linked with the class's implementation.
|
|---|
| 72 |
|
|---|
| 73 | In addition to providing the \l{signals and slots} mechanism for
|
|---|
| 74 | communication between objects (the main reason for introducing
|
|---|
| 75 | the system), the meta-object code provides the following
|
|---|
| 76 | additional features:
|
|---|
| 77 |
|
|---|
| 78 | \list
|
|---|
| 79 | \o QObject::metaObject() returns the associated
|
|---|
| 80 | \l{QMetaObject}{meta-object} for the class.
|
|---|
| 81 | \o QMetaObject::className() returns the class name as a
|
|---|
| 82 | string at run-time, without requiring native run-time type information
|
|---|
| 83 | (RTTI) support through the C++ compiler.
|
|---|
| 84 | \o QObject::inherits() function returns whether an object is an
|
|---|
| 85 | instance of a class that inherits a specified class within the
|
|---|
| 86 | QObject inheritance tree.
|
|---|
| 87 | \o QObject::tr() and QObject::trUtf8() translate strings for
|
|---|
| 88 | \l{Internationalization with Qt}{internationalization}.
|
|---|
| 89 | \o QObject::setProperty() and QObject::property()
|
|---|
| 90 | dynamically set and get properties by name.
|
|---|
| 91 | \o QMetaObject::newInstance() constructs a new instance of the class.
|
|---|
| 92 | \endlist
|
|---|
| 93 |
|
|---|
| 94 | \target qobjectcast
|
|---|
| 95 | It is also possible to perform dynamic casts using qobject_cast()
|
|---|
| 96 | on QObject classes. The qobject_cast() function behaves similarly
|
|---|
| 97 | to the standard C++ \c dynamic_cast(), with the advantages
|
|---|
| 98 | that it doesn't require RTTI support and it works across dynamic
|
|---|
| 99 | library boundaries. It attempts to cast its argument to the pointer
|
|---|
| 100 | type specified in angle-brackets, returning a non-zero pointer if the
|
|---|
| 101 | object is of the correct type (determined at run-time), or 0
|
|---|
| 102 | if the object's type is incompatible.
|
|---|
| 103 |
|
|---|
| 104 | For example, let's assume \c MyWidget inherits from QWidget and
|
|---|
| 105 | is declared with the Q_OBJECT macro:
|
|---|
| 106 |
|
|---|
| 107 | \snippet doc/src/snippets/qtcast/qtcast.cpp 0
|
|---|
| 108 |
|
|---|
| 109 | The \c obj variable, of type \c{QObject *}, actually refers to a
|
|---|
| 110 | \c MyWidget object, so we can cast it appropriately:
|
|---|
| 111 |
|
|---|
| 112 | \snippet doc/src/snippets/qtcast/qtcast.cpp 1
|
|---|
| 113 |
|
|---|
| 114 | The cast from QObject to QWidget is successful, because the
|
|---|
| 115 | object is actually a \c MyWidget, which is a subclass of QWidget.
|
|---|
| 116 | Since we know that \c obj is a \c MyWidget, we can also cast it to
|
|---|
| 117 | \c{MyWidget *}:
|
|---|
| 118 |
|
|---|
| 119 | \snippet doc/src/snippets/qtcast/qtcast.cpp 2
|
|---|
| 120 |
|
|---|
| 121 | The cast to \c MyWidget is successful because qobject_cast()
|
|---|
| 122 | makes no distinction between built-in Qt types and custom types.
|
|---|
| 123 |
|
|---|
| 124 | \snippet doc/src/snippets/qtcast/qtcast.cpp 3
|
|---|
| 125 | \snippet doc/src/snippets/qtcast/qtcast.cpp 4
|
|---|
| 126 |
|
|---|
| 127 | The cast to QLabel, on the other hand, fails. The pointer is then
|
|---|
| 128 | set to 0. This makes it possible to handle objects of different
|
|---|
| 129 | types differently at run-time, based on the type:
|
|---|
| 130 |
|
|---|
| 131 | \snippet doc/src/snippets/qtcast/qtcast.cpp 5
|
|---|
| 132 | \snippet doc/src/snippets/qtcast/qtcast.cpp 6
|
|---|
| 133 |
|
|---|
| 134 | While it is possible to use QObject as a base class without the
|
|---|
| 135 | Q_OBJECT macro and without meta-object code, neither signals
|
|---|
| 136 | and slots nor the other features described here will be available
|
|---|
| 137 | if the Q_OBJECT macro is not used. From the meta-object
|
|---|
| 138 | system's point of view, a QObject subclass without meta code is
|
|---|
| 139 | equivalent to its closest ancestor with meta-object code. This
|
|---|
| 140 | means for example, that QMetaObject::className() will not return
|
|---|
| 141 | the actual name of your class, but the class name of this
|
|---|
| 142 | ancestor.
|
|---|
| 143 |
|
|---|
| 144 | Therefore, we strongly recommend that all subclasses of QObject
|
|---|
| 145 | use the Q_OBJECT macro regardless of whether or not they
|
|---|
| 146 | actually use signals, slots, and properties.
|
|---|
| 147 |
|
|---|
| 148 | \sa QMetaObject, {Qt's Property System}, {Signals and Slots}
|
|---|
| 149 | */
|
|---|