Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
metaobjects.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page metaobjects.html
6 \title The Meta-Object System
7 \brief An overview of Qt's meta-object system and introspection capabilities.
8 \ingroup explanations-basics
9 \ingroup qt-basic-concepts
10 \keyword meta-object
11 \keyword Meta-Object System
12
13 Qt's meta-object system provides the signals and slots mechanism for
14 inter-object communication, run-time type information, and the dynamic
15 property system.
16
17 The meta-object system is based on three things:
18
19 \list 1
20 \li The \l QObject class provides a base class for objects that can
21 take advantage of the meta-object system.
22 \li The Q_OBJECT macro inside the private section of the class
23 declaration is used to enable meta-object features, such as
24 dynamic properties, signals, and slots.
25 \li The \l{moc}{Meta-Object Compiler} (\c moc) supplies each
26 QObject subclass with the necessary code to implement
27 meta-object features.
28 \endlist
29
30 The \c moc tool reads a C++ source file. If it finds one or more
31 class declarations that contain the Q_OBJECT macro, it
32 produces another C++ source file which contains the meta-object
33 code for each of those classes. This generated source file is
34 either \c{#include}'d into the class's source file or, more
35 usually, compiled and linked with the class's implementation.
36
37 In addition to providing the \l{signals and slots} mechanism for
38 communication between objects (the main reason for introducing
39 the system), the meta-object code provides the following
40 additional features:
41
42 \list
43 \li QObject::metaObject() returns the associated
44 \l{QMetaObject}{meta-object} for the class.
45 \li QMetaObject::className() returns the class name as a
46 string at run-time, without requiring native run-time type information
47 (RTTI) support through the C++ compiler.
48 \li QObject::inherits() function returns whether an object is an
49 instance of a class that inherits a specified class within the
50 QObject inheritance tree.
51 \li QObject::tr() translates strings for
52 \l{Internationalization with Qt}{internationalization}.
53 \li QObject::setProperty() and QObject::property()
54 dynamically set and get properties by name.
55 \li QMetaObject::newInstance() constructs a new instance of the class.
56 \endlist
57
58 \target qobjectcast
59 It is also possible to perform dynamic casts using qobject_cast()
60 on QObject classes. The qobject_cast() function behaves similarly
61 to the standard C++ \c dynamic_cast(), with the advantages
62 that it doesn't require RTTI support and it works across dynamic
63 library boundaries. It attempts to cast its argument to the pointer
64 type specified in angle-brackets, returning a non-zero pointer if the
65 object is of the correct type (determined at run-time), or \nullptr
66 if the object's type is incompatible.
67
68 For example, let's assume \c MyWidget inherits from QWidget and
69 is declared with the Q_OBJECT macro:
70
71 \snippet qtcast/qtcast.cpp 0
72
73 The \c obj variable, of type \c{QObject *}, actually refers to a
74 \c MyWidget object, so we can cast it appropriately:
75
76 \snippet qtcast/qtcast.cpp 1
77
78 The cast from QObject to QWidget is successful, because the
79 object is actually a \c MyWidget, which is a subclass of QWidget.
80 Since we know that \c obj is a \c MyWidget, we can also cast it to
81 \c{MyWidget *}:
82
83 \snippet qtcast/qtcast.cpp 2
84
85 The cast to \c MyWidget is successful because qobject_cast()
86 makes no distinction between built-in Qt types and custom types.
87
88 \snippet qtcast/qtcast.cpp 3
89 \snippet qtcast/qtcast.cpp 4
90
91 The cast to QLabel, on the other hand, fails. The pointer is then
92 set to 0. This makes it possible to handle objects of different
93 types differently at run-time, based on the type:
94
95 \snippet qtcast/qtcast.cpp 5
96 \snippet qtcast/qtcast.cpp 6
97
98 While it is possible to use QObject as a base class without the
99 Q_OBJECT macro and without meta-object code, neither signals
100 and slots nor the other features described here will be available
101 if the Q_OBJECT macro is not used. From the meta-object
102 system's point of view, a QObject subclass without meta code is
103 equivalent to its closest ancestor with meta-object code. This
104 means for example, that QMetaObject::className() will not return
105 the actual name of your class, but the class name of this
106 ancestor.
107
108 Therefore, we strongly recommend that all subclasses of QObject
109 use the Q_OBJECT macro regardless of whether or not they
110 actually use signals, slots, and properties.
111
112 \sa QMetaObject, {Qt's Property System}, {Signals and Slots}
113*/