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