summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-05-02 18:40:17 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-05-11 08:10:16 +0000
commitd026fad3d962eed0119351cd37f34490e09153fd (patch)
treef380549e86c77443f5387494f8142e254a9c19a3
parent8a873c2d6d0d70b2397046d3b72ab4a5a3530a91 (diff)
QPointer: also make conversion to pointer-to-const work
The QWeakPointer conversion SMFs cannot actually be used for QObject payloads, as, for unknown reasons (some comment about vtable this author doesn't understand), conversion goes through QSharedPointer, the creation of which throws the checkQObjectShared() warning and yields a nullptr. We need to continue to use the QWeakPointer(T*, bool) constructor the QPointer(T*) ctor also uses. It's high time we dissociated QPointer from QWeakPointer... Amends 5f28d367d999842a42fa0afa0d36d44ff61ea11d. Fixes: QTBUG-112464 Change-Id: I2f93843af3daf02323d77a4259eaa3745d8de3a8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/kernel/qpointer.h6
-rw-r--r--tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp4
2 files changed, 6 insertions, 4 deletions
diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h
index ada4445d43..66088054ef 100644
--- a/src/corelib/kernel/qpointer.h
+++ b/src/corelib/kernel/qpointer.h
@@ -33,9 +33,11 @@ public:
// compiler-generated dtor is fine!
template <typename X, if_convertible<X> = true>
- QPointer(QPointer<X> &&other) noexcept : wp(std::move(other.wp)) {}
+ QPointer(QPointer<X> &&other) noexcept
+ : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
template <typename X, if_convertible<X> = true>
- QPointer(const QPointer<X> &other) noexcept : wp(other.wp) {}
+ QPointer(const QPointer<X> &other) noexcept
+ : wp(other.wp.internalData(), true) {}
#ifdef Q_QDOC
// Stop qdoc from complaining about missing function
diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
index 0af3723704..332cf6ab71 100644
--- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
+++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
@@ -52,7 +52,7 @@ void tst_QPointer::conversion()
QFile file;
QPointer<QFile> pf = &file;
QCOMPARE_EQ(pf, &file);
- QPointer<QIODevice> pio = pf;
+ QPointer<const QIODevice> pio = pf;
QCOMPARE_EQ(pio, &file);
QCOMPARE_EQ(pio.get(), &file);
QCOMPARE_EQ(pio, pf);
@@ -63,7 +63,7 @@ void tst_QPointer::conversion()
QFile file;
QPointer<QFile> pf = &file;
QCOMPARE_EQ(pf, &file);
- QPointer<QIODevice> pio = std::move(pf);
+ QPointer<const QIODevice> pio = std::move(pf);
QCOMPARE_EQ(pf, nullptr);
QCOMPARE_EQ(pio, &file);
QCOMPARE_EQ(pio.get(), &file);