diff options
author | Olivier Goffart <ogoffart@woboq.com> | 2011-11-18 10:57:04 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-29 17:08:39 +0100 |
commit | ed0b262de97dd92c831127909ea4c059962b86ce (patch) | |
tree | b5ab25866220b0d0a136bd5b77dd227d55fcb0f8 /src/corelib/kernel/qobject.h | |
parent | a482487b9ff0cf0c47f9c0927c44019d52e2fdf8 (diff) |
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 <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/kernel/qobject.h')
-rw-r--r-- | src/corelib/kernel/qobject.h | 35 |
1 files changed, 35 insertions, 0 deletions
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 <typename Func1, typename Func2> + static inline bool disconnect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, + const typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot) + { + typedef QtPrivate::FunctionPointer<Func1> SignalType; + typedef QtPrivate::FunctionPointer<Func2> SlotType; + reinterpret_cast<typename SignalType::Object *>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<typename SignalType::Object *>(0)); + + //compilation error if the arguments does not match. + typedef typename QtPrivate::CheckCompatibleArguments<typename SignalType::Arguments, typename SlotType::Arguments>::IncompatibleSignalSlotArguments EnsureCompatibleArguments; + return disconnectImpl(sender, reinterpret_cast<void **>(&signal), receiver, reinterpret_cast<void **>(&slot), + &SignalType::Object::staticMetaObject); + } + template <typename Func1> + static inline bool disconnect(const typename QtPrivate::FunctionPointer<Func1>::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<Func1> SignalType; + return disconnectImpl(sender, reinterpret_cast<void **>(&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<Args, R>(function, static_cast<typename FuncType::Object *>(receiver), a); } + virtual bool compare(void **f) { + return *reinterpret_cast<Func *>(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, |