From ed0b262de97dd92c831127909ea4c059962b86ce Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 18 Nov 2011 10:57:04 +0100 Subject: QObject::disconnect with new syntax This add an overload to disconnect which is symetrical to the new syntax of connect. It is possible to diconnect connection like this: QObject::connect( sender, &Sender::valueChanged, receiver, &Receiver::updateValue ); QObject::disconnect( sender, &Sender::valueChanged, receiver, &Receiver::updateValue ); This overload only work with pointer to member function, and not static functions or functors. The test is copied from tst_QObject::disconnect(), just changed the syntax of the connection and disconnection Change-Id: Ia8f819100cb12098e32877522b97b732b1e676a8 Reviewed-by: Bradley T. Hughes --- src/corelib/kernel/qobject.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/corelib/kernel/qobject.h') diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 27165cdcc3..3b8803c1a6 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -273,6 +273,33 @@ public: { return disconnect(this, 0, receiver, member); } static bool disconnect(const QMetaObject::Connection &); + template + static inline bool disconnect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, + const typename QtPrivate::FunctionPointer::Object *receiver, Func2 slot) + { + typedef QtPrivate::FunctionPointer SignalType; + typedef QtPrivate::FunctionPointer SlotType; + reinterpret_cast(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast(0)); + + //compilation error if the arguments does not match. + typedef typename QtPrivate::CheckCompatibleArguments::IncompatibleSignalSlotArguments EnsureCompatibleArguments; + return disconnectImpl(sender, reinterpret_cast(&signal), receiver, reinterpret_cast(&slot), + &SignalType::Object::staticMetaObject); + } + template + static inline bool disconnect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, + const QObject *receiver, void **zero) + { + // This is the overload for when one wish to disconnect a signal from any slot. (slot=0) + // Since the function template parametter cannot be deduced from '0', we use a + // dummy void ** parametter that must be equal to 0 + Q_ASSERT(!zero); + typedef QtPrivate::FunctionPointer SignalType; + return disconnectImpl(sender, reinterpret_cast(&signal), receiver, zero, + &SignalType::Object::staticMetaObject); + } + + void dumpObjectTree(); void dumpObjectInfo(); @@ -340,6 +367,7 @@ private: QSlotObjectBase() : ref(1) {} virtual ~QSlotObjectBase(); virtual void call(QObject *receiver, void **a) = 0; + virtual bool compare(void **); }; // implementation of QSlotObjectBase for which the slot is a pointer to member function of a QObject // Args and R are the List of arguments and the returntype of the signal to which the slot is connected. @@ -351,6 +379,9 @@ private: virtual void call(QObject *receiver, void **a) { FuncType::template call(function, static_cast(receiver), a); } + virtual bool compare(void **f) { + return *reinterpret_cast(f) == function; + } }; // implementation of QSlotObjectBase for which the slot is a static function // Args and R are the List of arguments and the returntype of the signal to which the slot is connected. @@ -378,6 +409,10 @@ private: static QMetaObject::Connection connectImpl(const QObject *sender, void **signal, const QObject *receiver, QSlotObjectBase *slot, Qt::ConnectionType type, const int *types, const QMetaObject *senderMetaObject); + + static bool disconnectImpl(const QObject *sender, void **signal, const QObject *receiver, void **slot, + const QMetaObject *senderMetaObject); + }; inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, -- cgit v1.2.3