diff --git a/src/corelib/kernel/qpointer.cpp b/src/corelib/kernel/qpointer.cpp index c884844db3a..e426c63ae5f 100644 --- a/src/corelib/kernel/qpointer.cpp +++ b/src/corelib/kernel/qpointer.cpp @@ -111,6 +111,17 @@ is convertible to \c{T*}. */ +/*! + \fn template template QPointer &QPointer::operator=(const QPointer &other) + \since 6.6 + + Conversion assignment operator. Makes this guarded pointer guard the + same object guarded by \a other. + + \note This operator participates in overload resolution only if \c{X*} + is convertible to \c{T*}. +*/ + /*! \fn template void QPointer::swap(QPointer &other) \since 5.6 diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h index 1914c45190a..7de159a535c 100644 --- a/src/corelib/kernel/qpointer.h +++ b/src/corelib/kernel/qpointer.h @@ -43,6 +43,13 @@ public: QPointer(const QPointer &other) noexcept : wp(other.wp.internalData(), true) {} + template = true> + QPointer &operator=(const QPointer &other) + { + wp.assign(other.data()); + return *this; + } + #ifdef Q_QDOC // Stop qdoc from complaining about missing function ~QPointer(); diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index 332cf6ab717..7f01c6f964a 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -57,6 +57,19 @@ void tst_QPointer::conversion() QCOMPARE_EQ(pio.get(), &file); QCOMPARE_EQ(pio, pf); QCOMPARE_EQ(pio.get(), pf.get()); + + // reset + pio = nullptr; + QCOMPARE_EQ(pio, nullptr); + QCOMPARE_EQ(pio.get(), nullptr); + + // copy-assignment + QCOMPARE_EQ(pf, &file); + pio = pf; + QCOMPARE_EQ(pio, &file); + QCOMPARE_EQ(pio.get(), &file); + QCOMPARE_EQ(pio, pf); + QCOMPARE_EQ(pio.get(), pf.get()); } // move-conversion: { @@ -67,6 +80,16 @@ void tst_QPointer::conversion() QCOMPARE_EQ(pf, nullptr); QCOMPARE_EQ(pio, &file); QCOMPARE_EQ(pio.get(), &file); + + // reset + pio = nullptr; + QCOMPARE_EQ(pio, nullptr); + QCOMPARE_EQ(pio.get(), nullptr); + + // move-assignment + pio = QPointer(&file); + QCOMPARE_EQ(pio, &file); + QCOMPARE_EQ(pio.get(), &file); } }