From 16d412da4c0d7ef4776604b906fccb8132a7712d Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 9 Nov 2020 15:23:52 +0100 Subject: QSignalSpy: Use QMetaType instead of metatype id in initArgs The RegisterMethodArgumentMetaType had been changed to take a QMetaType instead of a type id in 0161f00e5043090f22b23de9822c09062b17d675. Unfortunately, the usage of it in QSignalSpy was missed. This patch adjusts the metacall to correctly use a QMetaType. Moreover, use parameterMetaType instead of parameterType to benefit from metatypes which are already resolved at compile time. Task-number: QTBUG-88260 Fixes: QTBUG-88356 Change-Id: Id8fa46581a005d62818971ea24d8aa2e39dcd6d0 Reviewed-by: Eskil Abrahamsen Blomfeldt Reviewed-by: Lars Knoll --- src/testlib/qsignalspy.h | 10 ++++------ tests/auto/testlib/qsignalspy/tst_qsignalspy.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/testlib/qsignalspy.h b/src/testlib/qsignalspy.h index 1d42995b4e..3ba56749ea 100644 --- a/src/testlib/qsignalspy.h +++ b/src/testlib/qsignalspy.h @@ -192,23 +192,21 @@ private: { args.reserve(member.parameterCount()); for (int i = 0; i < member.parameterCount(); ++i) { - int tp = member.parameterType(i); - if (tp == QMetaType::UnknownType && obj) { + QMetaType tp = member.parameterMetaType(i); + if (!tp.isValid() && obj) { void *argv[] = { &tp, &i }; QMetaObject::metacall(const_cast(obj), QMetaObject::RegisterMethodArgumentMetaType, member.methodIndex(), argv); - if (tp == -1) - tp = QMetaType::UnknownType; } - if (tp == QMetaType::UnknownType) { + if (!tp.isValid()) { qWarning("QSignalSpy: Unable to handle parameter '%s' of type '%s' of method '%s'," " use qRegisterMetaType to register it.", member.parameterNames().at(i).constData(), member.parameterTypes().at(i).constData(), member.name().constData()); } - args << tp; + args << tp.id(); } } diff --git a/tests/auto/testlib/qsignalspy/tst_qsignalspy.cpp b/tests/auto/testlib/qsignalspy/tst_qsignalspy.cpp index 9555c2a844..fcd33f5af4 100644 --- a/tests/auto/testlib/qsignalspy/tst_qsignalspy.cpp +++ b/tests/auto/testlib/qsignalspy/tst_qsignalspy.cpp @@ -57,6 +57,7 @@ private slots: void spyFunctionPointerWithBasicArgs(); void spyFunctionPointerWithPointers(); void spyFunctionPointerWithQtClasses(); + void spyFunctionPointerWithCustomClass(); void spyFunctionPointerWithBasicQtClasses(); void spyFunctionPointerWithQtTypedefs(); @@ -72,6 +73,8 @@ private slots: void spyOnMetaMethod_invalid_data(); }; +struct CustomType {}; + class QtTestObject: public QObject { Q_OBJECT @@ -152,6 +155,8 @@ void tst_QSignalSpy::spyWithPointers() QCOMPARE(*static_cast(args.at(1).constData()), &i2); } +struct CustomType2; + class QtTestObject2: public QObject { Q_OBJECT @@ -163,6 +168,8 @@ signals: void sig3(QObject *o); void sig4(QChar c); void sig5(const QVariant &v); + void sig6(CustomType ); + void sig7(CustomType2 *); }; void tst_QSignalSpy::spyWithBasicQtClasses() @@ -383,6 +390,23 @@ void tst_QSignalSpy::spyFunctionPointerWithQtClasses() QCOMPARE(qvariant_cast(spy3.value(0).value(0)), QChar('A')); } +void tst_QSignalSpy::spyFunctionPointerWithCustomClass() +{ + QtTestObject2 obj; + { + QSignalSpy spy(&obj, &QtTestObject2::sig6); + emit obj.sig6({}); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.at(0).count(), 1); + QCOMPARE(spy.at(0).at(0).typeName(), "CustomType"); + } + + { + QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QSignalSpy: Unable to handle parameter '' of type 'CustomType2*' of method 'sig7', use qRegisterMetaType to register it."); + QSignalSpy spy(&obj, &QtTestObject2::sig7); + } +} + void tst_QSignalSpy::spyFunctionPointerWithQtTypedefs() { QtTestObject3 obj; -- cgit v1.2.3