diff options
author | Simon Hausmann <simon.hausmann@digia.com> | 2013-06-06 13:51:19 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-06-07 16:19:05 +0200 |
commit | d8a6a67579fe5f3a6b70b6774b6eee00a55834d5 (patch) | |
tree | 5108cb24eee120e28817746086d4d9873e3147d4 /tests/auto/corelib | |
parent | aff03cbc276357b4db412ec8f8368e723f2a6277 (diff) |
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 <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
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<QmlReceiver*>(this_); return; + case Call: static_cast<QmlReceiver*>(this_)->callCount++; return; + case Compare: *ret = static_cast<QmlReceiver*>(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<void**>(&a))); + + sender.emitSignal1(); + QCOMPARE(receiver->callCount, 1); + + receiver->destroyIfLastRef(); +#else + QSKIP("Needs QT_BUILD_INTERNAL"); +#endif +} QTEST_MAIN(tst_QObject) #include "tst_qobject.moc" |