![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore More...
#include <qpointer.h>
Public Member Functions | |
Q_NODISCARD_CTOR | QPointer ()=default |
Q_NODISCARD_CTOR | QPointer (T *p) |
Constructs a guarded pointer that points to the same object that p points to. | |
template<typename X , if_convertible< X > = true> | |
Q_NODISCARD_CTOR | QPointer (QPointer< X > &&other) noexcept |
template<typename X , if_convertible< X > = true> | |
Q_NODISCARD_CTOR | QPointer (const QPointer< X > &other) noexcept |
void | swap (QPointer &other) noexcept |
QPointer< T > & | operator= (T *p) |
Assignment operator. | |
T * | data () const |
T * | get () const |
T * | operator-> () const |
Overloaded arrow operator; implements pointer semantics. | |
T & | operator* () const |
Dereference operator; implements pointer semantics. | |
operator T* () const | |
Cast operator; implements pointer semantics. | |
bool | isNull () const |
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwise returns false . | |
void | clear () |
Friends | |
template<typename X > | |
class | QPointer |
Constructs a guarded pointer with value \nullptr. | |
\inmodule QtCore
The QPointer class is a template class that provides guarded pointers to QObject.
A guarded pointer, QPointer<T>, behaves like a normal C++ pointer {T *}, except that it is automatically cleared when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases).
T
must be a subclass of QObject.
Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity.
Note that Qt 5 introduces a slight change in behavior when using QPointer.
\list
\endlist
Qt also provides QSharedPointer, an implementation of a reference-counted shared pointer object, which can be used to maintain a collection of references to an individual pointer.
Example:
\dots
If the QLabel is deleted in the meantime, the label
variable will hold \nullptr instead of an invalid address, and the last line will never be executed.
The functions and operators available with a QPointer are the same as those available with a normal unguarded pointer, except the pointer arithmetic operators ({+},
{-},
{++}, and
{–}), which are normally used only with arrays of objects.
Use QPointers like normal pointers and you will not need to read this class documentation.
For creating guarded pointers, you can construct or assign to them from a T* or from another guarded pointer of the same type. You can compare them with each other using operator==() and operator!=(), or test for \nullptr with isNull(). You can dereference them using either the *x
or the x->member
notation.
A guarded pointer will automatically cast to a T
*, so you can freely mix guarded and unguarded pointers. This means that if you have a QPointer<QWidget>, you can pass it to a function that requires a QWidget *. For this reason, it is of little value to declare functions to take a QPointer as a parameter; just use normal pointers. Use a QPointer when you are storing a pointer over time.
Note that class T
must inherit QObject, or a compilation or link error will result.
Definition at line 17 of file qpointer.h.
|
default |
|
inline |
Constructs a guarded pointer that points to the same object that p points to.
Definition at line 33 of file qpointer.h.
|
inlinenoexcept |
Definition at line 39 of file qpointer.h.
|
inlinenoexcept |
Conversion constructor. Constructs a new QPointer by moving or copying from other.
The moved-from QPointer is reset to nullptr.
{X*} is convertible to
{T*}. Definition at line 43 of file qpointer.h.
Clears this QPointer object.
Definition at line 70 of file qpointer.h.
References QWeakPointer< T >::clear().
Referenced by QDeclarativeGeoMap::~QDeclarativeGeoMap(), QDragManager::drag(), QXcbDrag::endDrag(), QQuickColorDialogImplPrivate::eyeDropperLeave(), QCocoaMenuBar::handleReparent(), QPcscSlot::processStateChange(), QMdiSubWindowPrivate::restoreFocus(), QtWaylandClient::QWaylandInputContext::setFocusObject(), QtWaylandClient::QWaylandInputMethodContext::setFocusObject(), QtWaylandClient::QWaylandInputContext::update(), and QtWaylandClient::QWaylandInputMethodContext::update().
Returns the pointer to the object being guarded.
Definition at line 56 of file qpointer.h.
Referenced by QtWayland::WlShellIntegration::WlShellIntegration(), QDeclarativeGeoMap::~QDeclarativeGeoMap(), QMenuPrivate::~QMenuPrivate(), QDeclarativeGeoMapQuickItem::afterChildrenChanged(), QCocoaMenuBar::cocoaWindow(), QQuick3DRepeater::componentComplete(), QDeclarativeGeoMapCopyrightNotice::connectMap(), QQuickStyleItem::control(), QGeoTileFetcherNokia::copyrightsFetched(), QQmlPreviewFileEngineHandler::create(), QWindowsSystemTrayIcon::createMenu(), QAbstractItemView::dataChanged(), QQuick3DParticleModelParticle::delegate(), QQuick3DObjectPrivate::derefSceneManager(), QDragManager::drag(), QAbstractItemViewPrivate::editor(), QQuickWidgetPrivate::ensureEngine(), QQuickItemGrabResultPrivate::ensureImageInCache(), QHostInfoResult::event(), QXcbDrag::eventFilter(), QQuickViewPrivate::execute(), QQuickWidgetPrivate::execute(), QWindowsSystemTrayIcon::formatDebug(), QGraphicsScenePrivate::gestureEventHandler(), QPointer< T >::get(), QGrabWindowSurfaceCapture::Grabber::grabFrame(), QXcbDrag::handleDrop(), QWidgetWindow::handleEnterLeaveEvent(), QXcbDrag::handleLeave(), QQuickOverlayPrivate::handleMouseEvent(), QTableViewPrivate::heightHintForIndex(), QQuickWidgetPrivate::init(), QQuickViewPrivate::init(), QDeclarativeGeoMap::initialize(), QPlaceManagerEngineNokiaV2::initializeCategories(), QQuickStyleItem::initStyleOptionBase(), QWindowsWindow::menuBar(), QtAndroidInput::mouseMove(), QtAndroidInput::mouseUp(), QtAndroidInput::mouseWheel(), QWindowsPopupMenu::notifyAboutToShow(), QWindowsPopupMenu::notifyTriggered(), QQmlDelegateModelPrivate::object(), QPointer< const QObject >::operator const QObject *(), QPointer< T >::operator*(), QPointer< T >::operator->(), QNetworkRequest::originatingObject(), QLibraryPrivate::pluginInstance(), QGuiApplicationPrivate::processWindowScreenChangedEvent(), QGuiApplicationPrivate::processWindowStateChangedEvent(), QWindowsOleDropSource::QueryContinueDrag(), QCalendarPopup::setCalendarWidget(), QQuick3DRepeater::setDelegate(), QtWaylandClient::QWaylandInputContext::setFocusObject(), QtWaylandClient::QWaylandInputMethodContext::setFocusObject(), QQuick3DRepeater::setModel(), QQuickMenuPrivate::setParentMenu(), QDeclarativeGeoMapQuickItem::setSourceItem(), QXcbDrag::startDrag(), QLibraryPrivate::unload(), QQuickTreeViewDelegatePrivate::updateIndicatorPointerHandlers(), QDeclarativeGeoMapQuickItem::updatePolish(), QGeoTileFetcherNokia::versionFetched(), QTableViewPrivate::widthHintForIndex(), QTreeViewPrivate::widthHintForIndex(), QWindowsMouseHandler::windowUnderMouse(), and QWindowsPointerHandler::windowUnderMouse().
|
inline |
Same as data(). This function is provided for STL compatibility.
Definition at line 58 of file qpointer.h.
References QPointer< T >::data().
Referenced by QQnxMediaPlayer::setAudioOutput().
|
inline |
Returns true
if the referenced object has been destroyed or if there is no referenced object; otherwise returns false
.
Definition at line 67 of file qpointer.h.
References QWeakPointer< T >::isNull().
Referenced by QCocoaMenuBar::~QCocoaMenuBar(), QMenuPrivate::~QMenuPrivate(), QGraphicsProxyWidgetPrivate::_q_removeWidgetSlot(), QAction::activate(), QHeaderDataProxyModel::columnCount(), QWindowsSystemTrayIcon::createMenu(), QHeaderDataProxyModel::data(), QPdfPageSelectorPrivate::documentStatusChanged(), QDragManager::drag(), QQuickWidgetPrivate::ensureEngine(), QGeoTiledMapNokia::evaluateCopyrights(), QDialog::exec(), QMenuPrivate::exec(), QQuickColorDialogImplPrivate::eyeDropperEnter(), QWidgetPrivate::handleClose(), QCocoaMenuBar::handleReparent(), QPcscSlot::hasCard(), QQuickWidgetPrivate::init(), QQuickViewPrivate::init(), QWaylandViewPrivate::markSurfaceAsDestroyed(), QNetworkReplyImplPrivate::metaDataChanged(), QApplication::notify(), QWindowsPopupMenu::notifyAboutToShow(), QWindowsPopupMenu::notifyTriggered(), QDBusConnectionPrivate::processFinishedCall(), QGuiApplicationPrivate::processSafeAreaMarginsChangedEvent(), QPcscSlot::processStateChange(), QGuiApplicationPrivate::processWindowDevicePixelRatioChangedEvent(), QWindowsOleDropSource::QueryContinueDrag(), QQuick3DObjectPrivate::refSceneManager(), QGeoTileRequestManagerPrivate::requestTiles(), QMdiSubWindowPrivate::restoreFocus(), RetryFuture::retry(), QHeaderDataProxyModel::rowCount(), QDeclarativeGeoMap::setCopyrightsVisible(), QWindowsMouseHandler::translateMouseEvent(), QWindowsPointerHandler::translateMouseEvent(), QQuickActionPrivate::trigger(), and QQmlOpenMetaObjectPrivate::Property::value().
|
inline |
Cast operator; implements pointer semantics.
Because of this function you can pass a QPointer<T> to a function where a T* is required.
Definition at line 64 of file qpointer.h.
|
inline |
Dereference operator; implements pointer semantics.
Just use this operator as you would with a normal C++ pointer.
Definition at line 62 of file qpointer.h.
References QPointer< T >::data().
|
inline |
Overloaded arrow operator; implements pointer semantics.
Just use this operator as you would with a normal C++ pointer.
Definition at line 60 of file qpointer.h.
References QPointer< T >::data().
Assignment operator.
This guarded pointer will now point to the same object that p points to.
Definition at line 53 of file qpointer.h.
|
inlinenoexcept |
Swaps the contents of this QPointer with the contents of other. This operation is very fast and never fails.
Definition at line 51 of file qpointer.h.
References other(), and QWeakPointer< T >::swap().
Constructs a guarded pointer with value \nullptr.
Definition at line 24 of file qpointer.h.