summaryrefslogtreecommitdiffstats
path: root/doc/src/objecttrees.qdoc
blob: 1a5ed920c5e4ab53e70ba68c5f6eb7f0356bf15f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\page objecttrees.html
\title Object Trees and Object Ownership
\ingroup architecture
\brief Information about the parent-child pattern used to describe
object ownership in Qt.

\section1 Overview

\link QObject QObjects\endlink organize themselves in object trees.
When you create a QObject with another object as parent, it's added to
the parent's \link QObject::children() children() \endlink list, and
is deleted when the parent is. It turns out that this approach fits
the needs of GUI objects very well. For example, a \l QShortcut
(keyboard shortcut) is a child of the relevant window, so when the
user closes that window, the shorcut is deleted too.

\l QWidget, the base class of everything that appears on the screen,
extends the parent-child relationship. A child normally also becomes a
child widget, i.e. it is displayed in its parent's coordinate system
and is graphically clipped by its parent's boundaries. For example,
when the application deletes a message box after it has been
closed, the message box's buttons and label are also deleted, just as
we'd want, because the buttons and label are children of the message
box.

You can also delete child objects yourself, and they will remove
themselves from their parents. For example, when the user removes a
toolbar it may lead to the application deleting one of its \l QToolBar
objects, in which case the tool bar's \l QMainWindow parent would
detect the change and reconfigure its screen space accordingly.

The debugging functions \l QObject::dumpObjectTree() and \l
QObject::dumpObjectInfo() are often useful when an application looks or
acts strangely.

\target note on the order of construction/destruction of QObjects
\section1 Construction/Destruction Order of QObjects

When \l {QObject} {QObjects} are created on the heap (i.e., created
with \e new), a tree can be constructed from them in any order, and
later, the objects in the tree can be destroyed in any order. When any
QObject in the tree is deleted, if the object has a parent, the
destructor automatically removes the object from its parent. If the
object has children, the destructor automatically deletes each
child. No QObject is deleted twice, regardless of the order of
destruction.

When \l {QObject} {QObjects} are created on the stack, the same
behavior applies. Normally, the order of destruction still doesn't
present a problem. Consider the following snippet:

\snippet doc/src/snippets/code/doc_src_objecttrees.qdoc 0

The parent, \c window, and the child, \c quit, are both \l {QObject}
{QObjects} because QPushButton inherits QWidget, and QWidget inherits
QObject. This code is correct: the destructor of \c quit is \e not
called twice because the C++ language standard \e {(ISO/IEC 14882:2003)}
specifies that destructors of local objects are called in the reverse
order of their constructors. Therefore, the destructor of
the child, \c quit, is called first, and it removes itself from its
parent, \c window, before the destructor of \c window is called.

But now consider what happens if we swap the order of construction, as
shown in this second snippet:

\snippet doc/src/snippets/code/doc_src_objecttrees.qdoc 1

In this case, the order of destruction causes a problem. The parent's
destructor is called first because it was created last. It then calls
the destructor of its child, \c quit, which is incorrect because \c
quit is a local variable. When \c quit subsequently goes out of scope,
its destructor is called again, this time correctly, but the damage has
already been done.

*/