summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-02-19 18:47:11 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2021-02-26 09:10:31 +0100
commit9d6ba110e8f98463d136fa97993db6d1968479c1 (patch)
tree501110a06ff6310d78fc0d176804c3a5af12d888 /tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
parent50e857a9cbb2d41a85e85e1c10e18bd761569b88 (diff)
QMetaType: Store QMetaObject for pointer to const QObject, too
Before this change, the QMetaType for T const* where T is derived from QObject would not store the static QMetaObject of T. This commit changes this. As a consequence, the metatype system can now convert between const and non-const pointers to QObject. Note that this allows casting const away; but so does C++ with const_cast. In addition, a new flag, QMetaType::IsImmutable is introduced, and used to tag the metatypes of pointer to const types. This allows code to discern between pointers to mutable and const QObjects, which is relevant for the QML engine. Task-number: QTBUG-82354 Change-Id: I3e4e4f39f565bd99a65e161528ce5304df73d6d6 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp')
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
index 3c097ad0d8..856b56941a 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
@@ -278,6 +278,20 @@ void tst_QMetaType::convertCustomType()
QCOMPARE(v.value<CustomConvertibleType2>().m_foo, testCustom.m_foo);
}
+void tst_QMetaType::convertConstNonConst()
+{
+ auto mtConstObj = QMetaType::fromType<QObject const*>();
+ auto mtObj = QMetaType::fromType<QObject *>();
+ auto mtConstDerived = QMetaType::fromType<Derived const*>();
+ auto mtDerived = QMetaType::fromType<Derived *>();
+
+ QVERIFY(QMetaType::canConvert(mtConstObj, mtObj));
+ QVERIFY(QMetaType::canConvert(mtObj, mtConstObj)); // casting const away is allowed (but can lead to UB)
+ QVERIFY(QMetaType::canConvert(mtConstDerived, mtObj));
+ QVERIFY(QMetaType::canConvert(mtDerived, mtConstObj));
+ QVERIFY(QMetaType::canConvert(mtObj, mtConstDerived));
+}
+
void tst_QMetaType::compareCustomEqualOnlyType()
{
QMetaType type = QMetaType::fromType<CustomEqualsOnlyType>();