1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
\brief The QPointer class is a template class that provides guarded pointers to QObjects.
36
A guarded pointer, QPointer<T>, behaves like a normal C++
37
pointer \c{T *}, except that it is automatically set to 0 when the
38
referenced object is destroyed (unlike normal C++ pointers, which
39
become "dangling pointers" in such cases). \c T must be a
42
Guarded pointers are useful whenever you need to store a pointer
43
to a QObject that is owned by someone else, and therefore might be
44
destroyed while you still hold a reference to it. You can safely
45
test the pointer for validity.
49
\quotefromfile snippets/pointer/pointer.cpp
50
\skipto QPointer<QLabel> label
56
If the QLabel is deleted in the meantime, the \c label variable
57
will hold 0 instead of an invalid address, and the last line will
60
The functions and operators available with a QPointer are the
61
same as those available with a normal unguarded pointer, except
62
the pointer arithmetic operators (\c{+}, \c{-}, \c{++}, and
63
\c{--}), which are normally used only with arrays of objects.
65
Use QPointers like normal pointers and you will not need to read
66
this class documentation.
68
For creating guarded pointers, you can construct or assign to them
69
from a T* or from another guarded pointer of the same type. You
70
can compare them with each other using operator==() and
71
operator!=(), or test for 0 with isNull(). You can dereference
72
them using either the \c *x or the \c x->member notation.
74
A guarded pointer will automatically cast to a \c T *, so you can
75
freely mix guarded and unguarded pointers. This means that if you
76
have a QPointer<QWidget>, you can pass it to a function that
77
requires a QWidget *. For this reason, it is of little value to
78
declare functions to take a QPointer as a parameter; just use
79
normal pointers. Use a QPointer when you are storing a pointer
82
Note that class \c T must inherit QObject, or a compilation or
83
link error will result.
85
\sa QObject, QObjectCleanupHandler
89
\fn QPointer::QPointer()
91
Constructs a 0 guarded pointer.
97
\fn QPointer::QPointer(T* p)
99
Constructs a guarded pointer that points to same object that \a p
104
\fn QPointer::QPointer(const QPointer<T> &p)
106
Copies one guarded pointer from another. The constructed guarded
107
pointer points to the same object that \a p points to (which may
112
\fn QPointer::~QPointer()
114
Destroys the guarded pointer. Just like a normal pointer,
115
destroying a guarded pointer does \e not destroy the object being
120
\fn QPointer<T>& QPointer::operator=(const QPointer<T> &p)
122
Assignment operator. This guarded pointer will now point to the
123
same object that \a p points to.
127
\fn QPointer<T> & QPointer::operator=(T* p)
131
Assignment operator. This guarded pointer will now point to the
132
same object that \a p points to.
136
\fn bool QPointer::isNull() const
138
Returns \c true if the referenced object has been destroyed or if
139
there is no referenced object; otherwise returns false.
143
\fn T* QPointer::operator->() const
145
Overloaded arrow operator; implements pointer semantics. Just use
146
this operator as you would with a normal C++ pointer.
150
\fn T& QPointer::operator*() const
152
Dereference operator; implements pointer semantics. Just use this
153
operator as you would with a normal C++ pointer.
157
\fn QPointer::operator T*() const
159
Cast operator; implements pointer semantics. Because of this
160
function you can pass a QPointer\<T\> to a function where a T*
165
\fn bool operator==(const T *o, const QPointer<T> &p)
167
Equality operator. Returns true if \a o and the guarded
168
pointer \a p are pointing to the same object, otherwise
173
\fn bool operator==(const QPointer<T> &p, const T *o)
175
Equality operator. Returns true if \a o and the guarded
176
pointer \a p are pointing to the same object, otherwise
181
\fn bool operator==(T *o, const QPointer<T> &p)
183
Equality operator. Returns true if \a o and the guarded
184
pointer \a p are pointing to the same object, otherwise
189
\fn bool operator==(const QPointer<T> &p, T *o)
191
Equality operator. Returns true if \a o and the guarded
192
pointer \a p are pointing to the same object, otherwise
197
\fn bool operator==(const QPointer<T> &p1, const QPointer<T> &p2)
199
Equality operator. Returns true if the guarded pointers \a p1 and \a p2
200
are pointing to the same object, otherwise
207
\fn bool operator!=(const T *o, const QPointer<T> &p)
209
Inequality operator. Returns true if \a o and the guarded
210
pointer \a p are not pointing to the same object, otherwise
214
\fn bool operator!=(const QPointer<T> &p, const T *o)
216
Inequality operator. Returns true if \a o and the guarded
217
pointer \a p are not pointing to the same object, otherwise
221
\fn bool operator!=(T *o, const QPointer<T> &p)
223
Inequality operator. Returns true if \a o and the guarded
224
pointer \a p are not pointing to the same object, otherwise
228
\fn bool operator!=(const QPointer<T> &p, T *o)
230
Inequality operator. Returns true if \a o and the guarded
231
pointer \a p are not pointing to the same object, otherwise
235
\fn bool operator!=(const QPointer<T> &p1, const QPointer<T> &p2)
237
Inequality operator. Returns true if the guarded pointers \a p1 and
238
\a p2 are not pointing to the same object, otherwise