source: trunk/doc/src/objecttrees.qdoc@ 109

Last change on this file since 109 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43\page objecttrees.html
44\title Object Trees and Object Ownership
45\ingroup architecture
46\brief Information about the parent-child pattern used to describe
47object ownership in Qt.
48
49\section1 Overview
50
51\link QObject QObjects\endlink organize themselves in object trees.
52When you create a QObject with another object as parent, it's added to
53the parent's \link QObject::children() children() \endlink list, and
54is deleted when the parent is. It turns out that this approach fits
55the needs of GUI objects very well. For example, a \l QShortcut
56(keyboard shortcut) is a child of the relevant window, so when the
57user closes that window, the shorcut is deleted too.
58
59\l QWidget, the base class of everything that appears on the screen,
60extends the parent-child relationship. A child normally also becomes a
61child widget, i.e. it is displayed in its parent's coordinate system
62and is graphically clipped by its parent's boundaries. For example,
63when the application deletes a message box after it has been
64closed, the message box's buttons and label are also deleted, just as
65we'd want, because the buttons and label are children of the message
66box.
67
68You can also delete child objects yourself, and they will remove
69themselves from their parents. For example, when the user removes a
70toolbar it may lead to the application deleting one of its \l QToolBar
71objects, in which case the tool bar's \l QMainWindow parent would
72detect the change and reconfigure its screen space accordingly.
73
74The debugging functions \l QObject::dumpObjectTree() and \l
75QObject::dumpObjectInfo() are often useful when an application looks or
76acts strangely.
77
78\target note on the order of construction/destruction of QObjects
79\section1 Construction/Destruction Order of QObjects
80
81When \l {QObject} {QObjects} are created on the heap (i.e., created
82with \e new), a tree can be constructed from them in any order, and
83later, the objects in the tree can be destroyed in any order. When any
84QObject in the tree is deleted, if the object has a parent, the
85destructor automatically removes the object from its parent. If the
86object has children, the destructor automatically deletes each
87child. No QObject is deleted twice, regardless of the order of
88destruction.
89
90When \l {QObject} {QObjects} are created on the stack, the same
91behavior applies. Normally, the order of destruction still doesn't
92present a problem. Consider the following snippet:
93
94\snippet doc/src/snippets/code/doc_src_objecttrees.qdoc 0
95
96The parent, \c window, and the child, \c quit, are both \l {QObject}
97{QObjects} because QPushButton inherits QWidget, and QWidget inherits
98QObject. This code is correct: the destructor of \c quit is \e not
99called twice because the C++ language standard \e {(ISO/IEC 14882:2003)}
100specifies that destructors of local objects are called in the reverse
101order of their constructors. Therefore, the destructor of
102the child, \c quit, is called first, and it removes itself from its
103parent, \c window, before the destructor of \c window is called.
104
105But now consider what happens if we swap the order of construction, as
106shown in this second snippet:
107
108\snippet doc/src/snippets/code/doc_src_objecttrees.qdoc 1
109
110In this case, the order of destruction causes a problem. The parent's
111destructor is called first because it was created last. It then calls
112the destructor of its child, \c quit, which is incorrect because \c
113quit is a local variable. When \c quit subsequently goes out of scope,
114its destructor is called again, this time correctly, but the damage has
115already been done.
116
117*/
Note: See TracBrowser for help on using the repository browser.