diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2024-12-16 17:16:57 +0100 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-12-19 22:30:37 +0000 |
| commit | 5fdc9ac9c53e93023dabd2dd309c7e86962ef842 (patch) | |
| tree | e08ce9b79874b2a1a58990b38aefc80cee0ba7d6 | |
| parent | 95bf6a9f26ed6cabd907ba3cd582a85ef1f5f263 (diff) | |
QObject: Suppress extra arguments on connections
If we use connect() to connect signals to other invokables we don't want
to see the warnings about extra signal arguments.
Amends commit cbe1869fbe7048f34513b201a1d9111a0a64257a.
Pick-to: 6.5
Fixes: QTBUG-131774
Change-Id: Iedec7b7c9f71cf2493ccdc00841e087bd336e028
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
(cherry picked from commit b7d1fafe5b72616964fb7338e26bfc3857e57490)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 0c53c4e3d888d296d9366552a07657a11e6cc150)
| -rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper.cpp | 22 | ||||
| -rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 6 |
2 files changed, 21 insertions, 7 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index dba0d36db4..9264b5ab0e 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -1205,6 +1205,7 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase PersistentValue function; PersistentValue thisObject; QMetaMethod signal; + qsizetype maxNumArguments; QObjectSlotDispatcher() : QtPrivate::QSlotObjectBase(&impl) @@ -1232,14 +1233,16 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase QQmlMetaObject::ArgTypeStorage<9> storage; QQmlMetaObject::methodParameterTypes(This->signal, &storage, nullptr); - int argCount = storage.size(); + const qsizetype argCount = std::min(storage.size(), This->maxNumArguments); Scope scope(v4); ScopedFunctionObject f(scope, This->function.value()); JSCallArguments jsCallData(scope, argCount); - *jsCallData.thisObject = This->thisObject.isUndefined() ? v4->globalObject->asReturnedValue() : This->thisObject.value(); - for (int ii = 0; ii < argCount; ++ii) { + *jsCallData.thisObject = This->thisObject.isUndefined() + ? v4->globalObject->asReturnedValue() + : This->thisObject.value(); + for (qsizetype ii = 0; ii < argCount; ++ii) { QMetaType type = storage[ii]; if (type == QMetaType::fromType<QVariant>()) { jsCallData.args[ii] = v4->fromVariant(*((QVariant *)metaArgs[ii + 1])); @@ -1379,8 +1382,21 @@ ReturnedValue QObjectWrapper::method_connect(const FunctionObject *b, const Valu receiver = typeWrapper->object(); if (receiver) { + if (functionData.second == -1) { + slot->maxNumArguments = std::numeric_limits<qsizetype>::max(); + } else { + // This means we are connecting to QObjectMethod which complains about extra arguments. + Heap::QObjectMethod *d = static_cast<Heap::QObjectMethod *>(f->d()); + d->ensureMethodsCache(receiver->metaObject()); + slot->maxNumArguments = std::accumulate(d->methods, d->methods + d->methodCount, 0, + [](int a, const QQmlPropertyData &b) { + return std::max(a, b.metaMethod().parameterCount()); + }); + } + QObjectPrivate::connect(signalObject, signalIndex, receiver, slot, Qt::AutoConnection); } else { + slot->maxNumArguments = std::numeric_limits<qsizetype>::max(); qCInfo(lcObjectConnect, "Could not find receiver of the connection, using sender as receiver. Disconnect " "explicitly (or delete the sender) to make sure the connection is removed."); diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index d69cd7eed7..4da1b52c5f 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -3744,6 +3744,8 @@ void tst_qqmlecmascript::attachedPropertyScope() void tst_qqmlecmascript::scriptConnect() { + QTest::failOnWarning(); + QQmlEngine engine; { @@ -3795,8 +3797,6 @@ void tst_qqmlecmascript::scriptConnect() QCOMPARE(object->methodCalled(), false); - QTest::ignoreMessage(QtWarningMsg, QRegularExpression("When matching arguments for MyQmlObject_QML_[0-9]+::methodNoArgs\\(\\):")); - QTest::ignoreMessage(QtWarningMsg, QRegularExpression("Too many arguments, ignoring 5")); emit object->argumentSignal(19, "Hello world!", 10.25, MyQmlObject::EnumValue4, Qt::RightButton); QCOMPARE(object->methodCalled(), true); } @@ -3810,8 +3810,6 @@ void tst_qqmlecmascript::scriptConnect() QVERIFY(object != nullptr); QCOMPARE(object->methodCalled(), false); - QTest::ignoreMessage(QtWarningMsg, QRegularExpression("When matching arguments for MyQmlObject_QML_[0-9]+::methodNoArgs\\(\\):")); - QTest::ignoreMessage(QtWarningMsg, QRegularExpression("Too many arguments, ignoring 5")); emit object->argumentSignal(19, "Hello world!", 10.25, MyQmlObject::EnumValue4, Qt::RightButton); QCOMPARE(object->methodCalled(), true); } |
