diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2012-04-22 22:44:58 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-27 10:37:02 +0200 |
commit | dac23b9a5700d3736cfb1aa2dccb1643f1122827 (patch) | |
tree | 82f00219fe8681a78b06bfb8c0d9fc077014a43e /src/corelib/kernel/qmetaobject.h | |
parent | 56d4d97852b37d9d1ca73d5b7f85e3865912f3db (diff) |
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 <ogoffart@woboq.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/kernel/qmetaobject.h')
-rw-r--r-- | src/corelib/kernel/qmetaobject.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/corelib/kernel/qmetaobject.h b/src/corelib/kernel/qmetaobject.h index 5204f04ca3..1c49506926 100644 --- a/src/corelib/kernel/qmetaobject.h +++ b/src/corelib/kernel/qmetaobject.h @@ -141,6 +141,20 @@ public: inline bool isValid() const { return mobj != 0; } +#ifdef Q_QDOC + static QMetaMethod fromSignal(PointerToMemberFunction signal); +#else + template <typename Func> + static inline QMetaMethod fromSignal(Func signal) + { + typedef QtPrivate::FunctionPointer<Func> SignalType; + reinterpret_cast<typename SignalType::Object *>(0)->qt_check_for_QOBJECT_macro( + *reinterpret_cast<typename SignalType::Object *>(0)); + return fromSignalImpl(&SignalType::Object::staticMetaObject, + reinterpret_cast<void **>(&signal)); + } +#endif + private: #if QT_DEPRECATED_SINCE(5,0) // signature() has been renamed to methodSignature() in Qt 5. @@ -148,6 +162,7 @@ private: // you convert to char*. char *signature(struct renamedInQt5_warning_checkTheLifeTime * = 0) Q_DECL_EQ_DELETE; #endif + static QMetaMethod fromSignalImpl(const QMetaObject *, void **); const QMetaObject *mobj; uint handle; |