From d8a6a67579fe5f3a6b70b6774b6eee00a55834d5 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 6 Jun 2013 13:51:19 +0200 Subject: Add internal functions to QObjectPrivate for signal & slot connections in QML In QML it is common to connect signals to slots that are implemented as JavaScript functions. QML used to maintain separate data structures that mirrored the QObject connection list and kept references to the JavaScript objects necessary to perform the call on signal activation. The recent addition of functor based QObject::connect makes it possible to store this information in QSlotObjectBase sub-class instead, which eliminates any extra bookkeeping. This patch adds internal connect and disconnect overloads to QObjectPrivate that allow for connecting QObject *sender, int signalIndex to a given QSlotObjectBase and similar for disconnect. Change-Id: I90f43d13eb95bd884d752484cf4faacc446f4d6a Reviewed-by: Olivier Goffart --- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index d16369de02..9bf28430b0 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -146,6 +146,7 @@ private slots: void connectFunctorOverloads(); void disconnectDoesNotLeakFunctor(); void connectBase(); + void qmlConnect(); }; struct QObjectCreatedOnShutdown @@ -5835,6 +5836,56 @@ void tst_QObject::connectBase() QCOMPARE( r1.count_slot3, 1 ); } +struct QmlReceiver : public QtPrivate::QSlotObjectBase +{ + int callCount; + void *magic; + + QmlReceiver() + : QtPrivate::QSlotObjectBase(&impl) + , callCount(0) + , magic(0) + {} + + static void impl(int which, QSlotObjectBase *this_, QObject *, void **metaArgs, bool *ret) + { + switch (which) { + case Destroy: delete static_cast(this_); return; + case Call: static_cast(this_)->callCount++; return; + case Compare: *ret = static_cast(this_)->magic == metaArgs[0]; return; + case NumOperations: break; + } + } +}; + +void tst_QObject::qmlConnect() +{ +#ifdef QT_BUILD_INTERNAL + SenderObject sender; + QmlReceiver *receiver = new QmlReceiver; + receiver->magic = receiver; + receiver->ref(); + + QVERIFY(QObjectPrivate::connect(&sender, sender.metaObject()->indexOfSignal("signal1()"), + receiver, Qt::AutoConnection)); + + QCOMPARE(receiver->callCount, 0); + sender.emitSignal1(); + QCOMPARE(receiver->callCount, 1); + + void *a[] = { + receiver + }; + QVERIFY(QObjectPrivate::disconnect(&sender, sender.metaObject()->indexOfSignal("signal1()"), reinterpret_cast(&a))); + + sender.emitSignal1(); + QCOMPARE(receiver->callCount, 1); + + receiver->destroyIfLastRef(); +#else + QSKIP("Needs QT_BUILD_INTERNAL"); +#endif +} QTEST_MAIN(tst_QObject) #include "tst_qobject.moc" -- cgit v1.2.3