1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
45
\brief The QPointer class is a template class that provides guarded pointers to QObject.
49
A guarded pointer, QPointer<T>, behaves like a normal C++
50
pointer \c{T *}, except that it is automatically set to 0 when the
51
referenced object is destroyed (unlike normal C++ pointers, which
52
become "dangling pointers" in such cases). \c T must be a
55
Guarded pointers are useful whenever you need to store a pointer
56
to a QObject that is owned by someone else, and therefore might be
57
destroyed while you still hold a reference to it. You can safely
58
test the pointer for validity.
60
Note that Qt 5 introduces a slight change in behavior when using QPointer.
64
\li When using QPointer on a QWidget (or a subclass of QWidget), previously
65
the QPointer would be cleared by the QWidget destructor. Now, the QPointer
66
is cleared by the QObject destructor (since this is when QWeakPointers are
67
cleared). Any QPointers tracking a widget will \b NOT be cleared before the
68
QWidget destructor destroys the children for the widget being tracked.
72
Qt also provides QSharedPointer, an implementation of a reference-counted
73
shared pointer object, which can be used to maintain a collection of
74
references to an individual pointer.
78
\snippet pointer/pointer.cpp 0
80
\snippet pointer/pointer.cpp 1
81
\snippet pointer/pointer.cpp 2
83
If the QLabel is deleted in the meantime, the \c label variable
84
will hold 0 instead of an invalid address, and the last line will
87
The functions and operators available with a QPointer are the
88
same as those available with a normal unguarded pointer, except
89
the pointer arithmetic operators (\c{+}, \c{-}, \c{++}, and
90
\c{--}), which are normally used only with arrays of objects.
92
Use QPointers like normal pointers and you will not need to read
93
this class documentation.
95
For creating guarded pointers, you can construct or assign to them
96
from a T* or from another guarded pointer of the same type. You
97
can compare them with each other using operator==() and
98
operator!=(), or test for 0 with isNull(). You can dereference
99
them using either the \c *x or the \c x->member notation.
101
A guarded pointer will automatically cast to a \c T *, so you can
102
freely mix guarded and unguarded pointers. This means that if you
103
have a QPointer<QWidget>, you can pass it to a function that
104
requires a QWidget *. For this reason, it is of little value to
105
declare functions to take a QPointer as a parameter; just use
106
normal pointers. Use a QPointer when you are storing a pointer
109
Note that class \c T must inherit QObject, or a compilation or
110
link error will result.
112
\sa QSharedPointer, QObject, QObjectCleanupHandler
116
\fn QPointer::QPointer()
118
Constructs a 0 guarded pointer.
124
\fn QPointer::QPointer(T* p)
126
Constructs a guarded pointer that points to the same object that \a p
131
\fn QPointer::~QPointer()
133
Destroys the guarded pointer. Just like a normal pointer,
134
destroying a guarded pointer does \e not destroy the object being
139
\fn QPointer<T> & QPointer::operator=(T* p)
141
Assignment operator. This guarded pointer will now point to the
142
same object that \a p points to.
146
\fn T* QPointer::data() const
149
Returns the pointer to the object being guarded.
153
\fn bool QPointer::isNull() const
155
Returns \c true if the referenced object has been destroyed or if
156
there is no referenced object; otherwise returns false.
160
\fn void QPointer::clear()
162
Clears this QPointer object.
168
\fn T* QPointer::operator->() const
170
Overloaded arrow operator; implements pointer semantics. Just use
171
this operator as you would with a normal C++ pointer.
175
\fn T& QPointer::operator*() const
177
Dereference operator; implements pointer semantics. Just use this
178
operator as you would with a normal C++ pointer.
182
\fn QPointer::operator T*() const
184
Cast operator; implements pointer semantics. Because of this
185
function you can pass a QPointer\<T\> to a function where a T*
190
\fn bool operator==(const T *o, const QPointer<T> &p)
193
Equality operator. Returns true if \a o and the guarded
194
pointer \a p are pointing to the same object, otherwise
199
\fn bool operator==(const QPointer<T> &p, const T *o)
202
Equality operator. Returns true if \a o and the guarded
203
pointer \a p are pointing to the same object, otherwise
208
\fn bool operator==(T *o, const QPointer<T> &p)
211
Equality operator. Returns true if \a o and the guarded
212
pointer \a p are pointing to the same object, otherwise
217
\fn bool operator==(const QPointer<T> &p, T *o)
220
Equality operator. Returns true if \a o and the guarded
221
pointer \a p are pointing to the same object, otherwise
226
\fn bool operator==(const QPointer<T> &p1, const QPointer<T> &p2)
229
Equality operator. Returns true if the guarded pointers \a p1 and \a p2
230
are pointing to the same object, otherwise
237
\fn bool operator!=(const T *o, const QPointer<T> &p)
240
Inequality operator. Returns true if \a o and the guarded
241
pointer \a p are not pointing to the same object, otherwise
245
\fn bool operator!=(const QPointer<T> &p, const T *o)
248
Inequality operator. Returns true if \a o and the guarded
249
pointer \a p are not pointing to the same object, otherwise
253
\fn bool operator!=(T *o, const QPointer<T> &p)
256
Inequality operator. Returns true if \a o and the guarded
257
pointer \a p are not pointing to the same object, otherwise
261
\fn bool operator!=(const QPointer<T> &p, T *o)
264
Inequality operator. Returns true if \a o and the guarded
265
pointer \a p are not pointing to the same object, otherwise
269
\fn bool operator!=(const QPointer<T> &p1, const QPointer<T> &p2)
272
Inequality operator. Returns true if the guarded pointers \a p1 and
273
\a p2 are not pointing to the same object, otherwise
277
\fn QPointer<T> qPointerFromVariant(const QVariant &variant)
281
Returns a guarded pointer that points to the same object that