diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-01-12 17:26:11 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-01-13 13:56:36 +0000 |
commit | 657a18c7faac28d7025350834dc7686e689ac358 (patch) | |
tree | 15db389a8ae47333b17500bdf8cefc17d87b7fcd /tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp | |
parent | 873a8edff8921636bef6112d545d91e1b04fe6ce (diff) |
QMetaType: Allow conversion of derived gadget types to their base types
A derived gadget has an is-a relationship with its base type. It
should be convertible. In fact, canConvert() already tells us it is.
Change-Id: I71a5ac9afd78e88adb23b4d0e757f34077f63207
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp index 8567288c8b..025353d9c0 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp @@ -164,6 +164,7 @@ void tst_QMetaType::convertCustomType_data() QTest::addColumn<QLineF>("testQLineF"); QTest::addColumn<QChar>("testQChar"); QTest::addColumn<CustomConvertibleType>("testCustom"); + QTest::addColumn<DerivedGadgetType>("testDerived"); QTest::newRow("default") << true << QString::fromLatin1("string") << true << 15 @@ -171,14 +172,16 @@ void tst_QMetaType::convertCustomType_data() << QRectF(1.4, 1.9, 10.9, 40.2) << QPoint(12, 34) << QPointF(9.2, 2.7) << QSize(4, 9) << QSizeF(3.3, 9.8) << QLine(3, 9, 29, 4) << QLineF(38.9, 28.9, 102.3, 0.0) - << QChar('Q') << CustomConvertibleType(QString::fromLatin1("test")); + << QChar('Q') << CustomConvertibleType(QString::fromLatin1("test")) + << DerivedGadgetType(QString::fromLatin1("test")); QTest::newRow("not ok") << false << QString::fromLatin1("string") << true << 15 << double(3.14) << float(3.6) << QRect(1, 2, 3, 4) << QRectF(1.4, 1.9, 10.9, 40.2) << QPoint(12, 34) << QPointF(9.2, 2.7) << QSize(4, 9) << QSizeF(3.3, 9.8) << QLine(3, 9, 29, 4) << QLineF() - << QChar('Q') << CustomConvertibleType(42); + << QChar('Q') << CustomConvertibleType(42) + << DerivedGadgetType(42); } void tst_QMetaType::convertCustomType() @@ -276,6 +279,15 @@ void tst_QMetaType::convertCustomType() v = QVariant::fromValue(testCustom); QVERIFY(v.canConvert(::qMetaTypeId<CustomConvertibleType2>())); QCOMPARE(v.value<CustomConvertibleType2>().m_foo, testCustom.m_foo); + + QFETCH(DerivedGadgetType, testDerived); + v = QVariant::fromValue(testDerived); + QCOMPARE(v.metaType(), QMetaType::fromType<DerivedGadgetType>()); + QCOMPARE(v.value<DerivedGadgetType>().m_foo, testDerived.m_foo); + QVERIFY(v.canConvert(QMetaType::fromType<BaseGadgetType>())); + QVERIFY(v.convert(QMetaType::fromType<BaseGadgetType>())); + QCOMPARE(v.metaType(), QMetaType::fromType<BaseGadgetType>()); + QCOMPARE(v.value<BaseGadgetType>().m_foo, testDerived.m_foo); } void tst_QMetaType::convertConstNonConst() |