diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 23ee85ee46..2883a47852 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -59,6 +59,7 @@ #include <QEasingCurve> #include <QSequentialIterable> #include <QAssociativeIterable> +#include <QScopeGuard> #include "qnumeric.h" #include <private/qlocale_p.h> @@ -253,6 +254,7 @@ private slots: void convertByteArrayToBool() const; void convertByteArrayToBool_data() const; void convertIterables() const; + void convertConstNonConst() const; void toIntFromQString() const; void toIntFromDouble() const; void setValue(); @@ -3136,6 +3138,34 @@ void tst_QVariant::convertIterables() const } } +struct Derived : QObject +{ + Q_OBJECT +}; + +void tst_QVariant::convertConstNonConst() const +{ + Derived *derived = new Derived; + QObject *obj = derived; + QObject const *unrelatedConstObj = new QObject; + auto cleanUp = qScopeGuard([&] { + delete unrelatedConstObj; + delete derived; + }); + QObject const *constObj = obj; + Derived const *constDerived = derived; + QCOMPARE(QVariant::fromValue(constObj).value<QObject *>(), obj); + QCOMPARE(QVariant::fromValue(obj).value<QObject const *>(), constObj); + + QCOMPARE(QVariant::fromValue(constDerived).value<QObject *>(), derived); + QCOMPARE(QVariant::fromValue(derived).value<QObject const *>(), derived); + + QObject const *derivedAsConstObject = derived; + // up cast and remove const is possible, albeit dangerous + QCOMPARE(QVariant::fromValue(derivedAsConstObject).value<Derived *>(), derived); + QCOMPARE(QVariant::fromValue(unrelatedConstObj).value<Derived *>(), nullptr); +} + /*! We verify that: 1. Converting the string "9.9" to int fails. This is the behavior of |