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