From dac23b9a5700d3736cfb1aa2dccb1643f1122827 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Sun, 22 Apr 2012 22:44:58 +0200 Subject: Add QMetaMethod::fromSignal() function Given a member function that's a signal, returns the corresponding QMetaMethod. Inspired by the implementation of the template-based QObject::connect(). The primary use case for this function is to have an effective and exact (not subject to shadowing) way of checking whether a known signal was connected to in reimplementations of QObject::connectNotify(QMetaMethod), avoiding string comparisons. Example: void MyObject::connectNotify(const QMetaMethod &signal) { if (signal == QMetaMethod::fromSignal(&MyObject::mySignal)) { // Someone connected to mySignal ... } } Change-Id: I5e4de434275fe543c004d569dcaa9ceda3442f03 Reviewed-by: Olivier Goffart Reviewed-by: Bradley T. Hughes --- .../corelib/kernel/qmetamethod/tst_qmetamethod.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp index b5ab61443c..2858bf64eb 100644 --- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp +++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp @@ -56,6 +56,8 @@ private slots: void invalidMethod(); void comparisonOperators(); + + void fromSignal(); }; struct CustomType { }; @@ -709,5 +711,30 @@ void tst_QMetaMethod::comparisonOperators() } } +void tst_QMetaMethod::fromSignal() +{ +#define FROMSIGNAL_HELPER(ObjectType, Name, Arguments) { \ + void (ObjectType::*signal)Arguments = &ObjectType::Name; \ + const QMetaObject *signalMeta = &ObjectType::staticMetaObject; \ + QCOMPARE(QMetaMethod::fromSignal(signal), \ + signalMeta->method(signalMeta->indexOfSignal(QMetaObject::normalizedSignature(#Name #Arguments)))); \ + } + + FROMSIGNAL_HELPER(MethodTestObject, voidSignal, ()) + FROMSIGNAL_HELPER(MethodTestObject, voidSignalQString, (const QString&)) + FROMSIGNAL_HELPER(QObject, destroyed, (QObject*)) + FROMSIGNAL_HELPER(QObject, objectNameChanged, (const QString &)) + + // Inherited from QObject + FROMSIGNAL_HELPER(MethodTestObject, destroyed, (QObject*)) + FROMSIGNAL_HELPER(MethodTestObject, objectNameChanged, (const QString &)) + + // Methods that are not signals; fromSignal should return invalid method + FROMSIGNAL_HELPER(MethodTestObject, voidSlot, ()) + FROMSIGNAL_HELPER(QObject, deleteLater, ()) + +#undef FROMSIGNAL_HELPER +} + QTEST_MAIN(tst_QMetaMethod) #include "tst_qmetamethod.moc" -- cgit v1.2.3