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 5f28d367d9.

Fixes: QTBUG-112464
Change-Id: I2f93843af3daf02323d77a4259eaa3745d8de3a8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2023-05-02 18:40:17 +02:00
parent 8a873c2d6d
commit d026fad3d9
2 changed files with 6 additions and 4 deletions

View File

@ -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

View File

@ -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);