QSignalSpy: Use QMetaType instead of metatype id in initArgs

The RegisterMethodArgumentMetaType had been changed to take a QMetaType
instead of a type id in 0161f00e50.
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 <eskil.abrahamsen-blomfeldt@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Fabian Kosmale 2020-11-09 15:23:52 +01:00
parent f95691b8af
commit 16d412da4c
2 changed files with 28 additions and 6 deletions

View File

@ -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<QObject*>(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();
}
}

View File

@ -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<int * const *>(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<QChar>(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;