From a539ce478800c3dacf8456d206e8fd6dc700ac5c Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Fri, 5 Jun 2009 17:38:39 +0200 Subject: make signal handlers understand QVariant again Also, issue a warning if a type is not known to the meta-type system. Backport of 508c9cd681244a5ad566c12733aa70f5bd522b57 Task-number: QTBUG-5060 --- src/script/qscriptextqobject.cpp | 21 +++++++++++++--- .../qscriptextqobject/tst_qscriptextqobject.cpp | 29 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/script/qscriptextqobject.cpp b/src/script/qscriptextqobject.cpp index b3b9a548..4be6b72 100644 --- a/src/script/qscriptextqobject.cpp +++ b/src/script/qscriptextqobject.cpp @@ -1657,12 +1657,27 @@ void QScript::QObjectConnectionManager::execute(int slotIndex, void **argv) activation_data->m_members[i].object(nameId, i, QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); + QScriptValueImpl actual; if (i < argc) { - int argType = QMetaType::type(parameterTypes.at(i)); - activation_data->m_values[i] = eng->create(argType, argv[i + 1]); + void *arg = argv[i + 1]; + QByteArray typeName = parameterTypes.at(i); + int argType = QMetaType::type(typeName); + if (!argType) { + if (typeName == "QVariant") { + actual = eng->valueFromVariant(*reinterpret_cast(arg)); + } else { + qWarning("QScriptEngine: Unable to handle unregistered datatype '%s' " + "when invoking handler of signal %s::%s", + typeName.constData(), meta->className(), method.signature()); + actual = eng->undefinedValue(); + } + } else { + actual = eng->create(argType, arg); + } } else { - activation_data->m_values[i] = eng->undefinedValue(); + actual = eng->undefinedValue(); } + activation_data->m_values[i] = actual; } QScriptValueImpl senderObject; diff --git a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp index 8768d0e..5816b94 100644 --- a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp +++ b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp @@ -326,6 +326,10 @@ public: { emit mySignalWithDefaultArg(arg); } void emitMySignalWithDefaultArg() { emit mySignalWithDefaultArg(); } + void emitMySignalWithVariantArg(const QVariant &arg) + { emit mySignalWithVariantArg(arg); } + void emitMySignalWithScriptEngineArg(QScriptEngine *arg) + { emit mySignalWithScriptEngineArg(arg); } public Q_SLOTS: void mySlot() @@ -388,6 +392,8 @@ Q_SIGNALS: void myOtherOverloadedSignal(const QString &arg); void myOtherOverloadedSignal(int arg); void mySignalWithDefaultArg(int arg = 123); + void mySignalWithVariantArg(const QVariant &arg); + void mySignalWithScriptEngineArg(QScriptEngine *arg); protected: void connectNotify(const char *signal) { @@ -1501,6 +1507,29 @@ void tst_QScriptExtQObject::connectAndDisconnect() m_myObject->emitMyOtherOverloadedSignal(123); QVERIFY(!m_engine->evaluate("gotSignal").toBoolean()); + // signal with QVariant arg: argument conversion should work + m_myObject->clearConnectedSignal(); + QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.connect(myHandler)").isUndefined()); + QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithVariantArg(QVariant))); + m_engine->evaluate("gotSignal = false"); + m_myObject->emitMySignalWithVariantArg(123); + QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true); + QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0); + QCOMPARE(m_engine->evaluate("signalArgs[0]").toNumber(), 123.0); + QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.disconnect(myHandler)").isUndefined()); + + // signal with argument type that's unknown to the meta-type system + m_myObject->clearConnectedSignal(); + QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.connect(myHandler)").isUndefined()); + QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithScriptEngineArg(QScriptEngine*))); + m_engine->evaluate("gotSignal = false"); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine: Unable to handle unregistered datatype 'QScriptEngine*' when invoking handler of signal MyQObject::mySignalWithScriptEngineArg(QScriptEngine*)"); + m_myObject->emitMySignalWithScriptEngineArg(m_engine); + QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true); + QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0); + QVERIFY(m_engine->evaluate("signalArgs[0]").isUndefined()); + QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.disconnect(myHandler)").isUndefined()); + // connect(object, function) m_engine->evaluate("otherObject = { name:'foo' }"); QVERIFY(m_engine->evaluate("myObject.mySignal.connect(otherObject, myHandler)").isUndefined()); -- cgit v0.12