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
qpointer.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
\class QPointer
6
\inmodule QtCore
7
\brief The QPointer class is a template class that provides guarded pointers to QObject.
8
9
\ingroup objectmodel
10
11
\compares equality
12
\compareswith equality QPointer<X> X* std::nullptr_t
13
Where X and T are compatible types, which means that they are either the same (except
14
for their cv-qualifiers), or one is a base type of the other.
15
\endcompareswith
16
17
A guarded pointer, QPointer<T>, behaves like a normal C++
18
pointer \c{T *}, except that it is automatically cleared when the
19
referenced object is destroyed (unlike normal C++ pointers, which
20
become "dangling pointers" in such cases). \c T must be a
21
subclass of QObject.
22
23
Guarded pointers are useful whenever you need to store a pointer
24
to a QObject that is owned by someone else, and therefore might be
25
destroyed while you still hold a reference to it. You can safely
26
test the pointer for validity.
27
28
Note that Qt 5 introduces a slight change in behavior when using QPointer.
29
30
\list
31
32
\li When using QPointer on a QWidget (or a subclass of QWidget), previously
33
the QPointer would be cleared by the QWidget destructor. Now, the QPointer
34
is cleared by the QObject destructor (since this is when QWeakPointer objects are
35
cleared). Any QPointers tracking a widget will \b NOT be cleared before the
36
QWidget destructor destroys the children for the widget being tracked.
37
38
\endlist
39
40
Qt also provides QSharedPointer, an implementation of a reference-counted
41
shared pointer object, which can be used to maintain a collection of
42
references to an individual pointer.
43
44
Example:
45
46
\snippet pointer/pointer.cpp 0
47
\dots
48
\snippet pointer/pointer.cpp 1
49
\snippet pointer/pointer.cpp 2
50
51
If the QLabel is deleted in the meantime, the \c label variable
52
will hold \nullptr instead of an invalid address, and the last line will
53
never be executed.
54
55
The functions and operators available with a QPointer are the
56
same as those available with a normal unguarded pointer, except
57
the pointer arithmetic operators (\c{+}, \c{-}, \c{++}, and
58
\c{--}), which are normally used only with arrays of objects.
59
60
Use QPointers like normal pointers and you will not need to read
61
this class documentation.
62
63
For creating guarded pointers, you can construct or assign to them
64
from a T* or from another guarded pointer of the same type. You
65
can compare them with each other using operator==() and
66
operator!=(), or test for \nullptr with isNull(). You can dereference
67
them using either the \c *x or the \c x->member notation.
68
69
A guarded pointer will automatically cast to a \c T *, so you can
70
freely mix guarded and unguarded pointers. This means that if you
71
have a QPointer<QWidget>, you can pass it to a function that
72
requires a QWidget *. For this reason, it is of little value to
73
declare functions to take a QPointer as a parameter; just use
74
normal pointers. Use a QPointer when you are storing a pointer
75
over time.
76
77
Note that class \c T must inherit QObject, or a compilation or
78
link error will result.
79
80
\sa QSharedPointer, QObject, QObjectCleanupHandler
81
*/
82
83
/*!
84
\fn template <class T> QPointer<T>::QPointer()
85
\fn template <class T> QPointer<T>::QPointer(std::nullptr_t)
86
87
Constructs a guarded pointer with value \nullptr.
88
89
\sa isNull()
90
*/
91
92
/*!
93
\fn template <class T> QPointer<T>::QPointer(T* p)
94
95
Constructs a guarded pointer that points to the same object that \a p
96
points to.
97
*/
98
99
/*!
100
\fn template <class T> QPointer<T>::~QPointer()
101
102
Destroys the guarded pointer. Just like a normal pointer,
103
destroying a guarded pointer does \e not destroy the object being
104
pointed to.
105
*/
106
107
/*!
108
\fn template <class T> template <typename X, QPointer<T>::if_convertible<X> = true> QPointer<T>::QPointer(QPointer<X> &&other)
109
\fn template <class T> template <typename X, QPointer<T>::if_convertible<X> = true> QPointer<T>::QPointer(const QPointer<X> &other)
110
\since 6.6
111
112
Conversion constructor. Constructs a new QPointer by moving or copying from
113
\a other.
114
115
The moved-from QPointer is reset to nullptr.
116
117
\note These constructors participate in overload resolution only if \c{X*}
118
is convertible to \c{T*}.
119
*/
120
121
/*!
122
\fn template <class T> template <typename X, QPointer<T>::if_convertible<X> = true> QPointer<T> &QPointer<T>::operator=(const QPointer<X> &other)
123
\since 6.6
124
125
Conversion assignment operator. Makes this guarded pointer guard the
126
same object guarded by \a other.
127
128
\constraints \c{X*} is convertible to \c{T*}.
129
*/
130
131
/*!
132
\fn template <class T> template <typename X, QPointer<T>::if_convertible<X> = true> &QPointer<T>::operator=(QPointer<X> &&other)
133
\since 6.6.1
134
135
Conversion move-assignment operator. Makes this guarded pointer guard the
136
same object guarded by \a other and resets \a other to nullptr.
137
138
\constraints \c{X*} is convertible to \c{T*}.
139
*/
140
141
/*!
142
\fn template <class T> void QPointer<T>::swap(QPointer &other)
143
\since 5.6
144
\memberswap{pointer}
145
*/
146
147
/*!
148
\fn template <class T> QPointer<T> & QPointer<T>::operator=(T* p)
149
150
Assignment operator. This guarded pointer will now point to the
151
same object that \a p points to.
152
*/
153
154
/*!
155
\fn template <class T> T* QPointer<T>::data() const
156
\since 4.4
157
158
Returns the pointer to the object being guarded.
159
*/
160
161
/*!
162
\fn template <class T> T* QPointer<T>::get() const
163
\since 6.0
164
165
Same as data(). This function is provided for STL compatibility.
166
*/
167
168
/*!
169
\fn template <class T> bool QPointer<T>::isNull() const
170
171
Returns \c true if the referenced object has been destroyed or if
172
there is no referenced object; otherwise returns \c false.
173
*/
174
175
/*!
176
\fn template <class T> void QPointer<T>::clear()
177
\since 5.0
178
179
Clears this QPointer object.
180
181
\sa isNull()
182
*/
183
184
/*!
185
\fn template <class T> T* QPointer<T>::operator->() const
186
187
Overloaded arrow operator; implements pointer semantics. Just use
188
this operator as you would with a normal C++ pointer.
189
*/
190
191
/*!
192
\fn template <class T> T& QPointer<T>::operator*() const
193
194
Dereference operator; implements pointer semantics. Just use this
195
operator as you would with a normal C++ pointer.
196
*/
197
198
/*!
199
\fn template <class T> QPointer<T>::operator T*() const
200
201
Cast operator; implements pointer semantics. Because of this
202
function you can pass a QPointer<T> to a function where a T*
203
is required.
204
*/
205
206
/*!
207
\fn template <typename T> template<typename X> bool QPointer<T>::operator==(X* const &lhs, const QPointer<T> &rhs)
208
209
Equality operator. Returns \c true if \a lhs and the guarded
210
pointer \a rhs are pointing to the same object, otherwise
211
returns \c false.
212
213
*/
214
/*!
215
\fn template <typename T> template<typename X> bool QPointer<T>::operator==(const QPointer<T> &lhs, X* const &rhs)
216
217
Equality operator. Returns \c true if \a rhs and the guarded
218
pointer \a lhs are pointing to the same object, otherwise
219
returns \c false.
220
221
*/
222
/*!
223
\fn template <typename T> template<typename X> bool QPointer<T>::operator==(const QPointer<T> &lhs, const QPointer<X> &rhs)
224
225
Equality operator. Returns \c true if the guarded pointers \a lhs and \a rhs
226
are pointing to the same object, otherwise
227
returns \c false.
228
229
*/
230
/*!
231
\fn template <typename T> bool QPointer<T>::operator==(std::nullptr_t const &lhs, const QPointer<T> &rhs)
232
233
Equality operator. Returns \c true if the pointer guarded by \a rhs
234
is \nullptr, otherwise
235
returns \c false.
236
*/
237
/*!
238
\fn template <typename T> bool QPointer<T>::operator==(const QPointer<T> &lhs, std::nullptr_t const &rhs)
239
240
Equality operator. Returns \c true if the pointer guarded by \a lhs
241
is \nullptr, otherwise
242
returns \c false.
243
*/
244
245
/*!
246
\fn template <typename T> template<typename X> bool QPointer<T>::operator!=(const QPointer<T> &lhs, X* const &rhs)
247
248
Inequality operator. Returns \c true if \a rhs and the guarded
249
pointer \a lhs are not pointing to the same object, otherwise
250
returns \c false.
251
*/
252
/*!
253
\fn template <typename T> template<typename X> bool QPointer<T>::operator!=(X* const &lhs, const QPointer<T> &rhs)
254
255
Inequality operator. Returns \c true if \a lhs and the guarded
256
pointer \a rhs are not pointing to the same object, otherwise
257
returns \c false.
258
*/
259
/*!
260
\fn template <typename T> template<typename X> bool QPointer<T>::operator!=(const QPointer<T> &lhs, const QPointer<X> &rhs)
261
262
Inequality operator. Returns \c true if the guarded pointers \a lhs and
263
\a rhs are not pointing to the same object, otherwise
264
returns \c false.
265
*/
266
/*!
267
\fn template <typename T> bool QPointer<T>::operator!=(std::nullptr_t const &lhs, const QPointer<T> &rhs)
268
269
Inequality operator. Returns \c true if the pointer guarded by \a rhs is
270
a valid (ie not \nullptr) pointer, otherwise
271
returns \c false.
272
*/
273
/*!
274
\fn template <typename T> bool QPointer<T>::operator!=(const QPointer<T> &lhs, std::nullptr_t const &rhs)
275
276
Inequality operator. Returns \c true if the pointer guarded by \a lhs is
277
a valid (ie not \nullptr) pointer, otherwise
278
returns \c false.
279
*/
280
281
/*!
282
\fn template <typename T> QPointer<T> qPointerFromVariant(const QVariant &variant)
283
284
\internal
285
286
Returns a guarded pointer that points to the same object that
287
\a variant holds.
288
*/
qtbase
src
corelib
kernel
qpointer.qdoc
Generated on
for Qt by
1.14.0