diff options
author | Dario Freddi <dario.freddi@ispirata.com> | 2013-07-17 00:39:23 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-16 23:46:30 +0200 |
commit | 76b06f993e534e02e35830ac0d8f41e546463945 (patch) | |
tree | 08a76949780dc0e74214c3052efbedaa741fc4fb /src/corelib/kernel/qobject.cpp | |
parent | ae577e6b8f8b966cf87293b12803bcc813019a2e (diff) |
QObject: allow connecting to functors with a receiver object
Up to now, it was only possible to connect to functors in a direct
way, without being capable of using Qt::ConnectionType. This patch
allows for specifying a receiver for Functors and function
pointers, hence making it possible to specify effectively the
connection type.
To do this properly, it was needed to add an enum in FunctionPointer
representing whether the considered function is a member function
or not, to reduce ambiguity upon overloaded calls.
Moreover, now senders are checked for the existence of a slot obj
as well. This way, should the context be freed, the slot obj and
the functor contained in it will be freed as well.
On a side note, connecting to a static slot
(like QCoreApplication::quit) specifying the receiver object is
now compiling.
Change-Id: I46474099413b1dc6ca4db9934191d469baeef070
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/kernel/qobject.cpp')
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index b914ca812f..cc689657d2 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -854,9 +854,23 @@ QObject::~QObject() senderLists->dirty = true; int signal_index = node->signal_index; + + QtPrivate::QSlotObjectBase *slotObj = Q_NULLPTR; + if (node->isSlotObject) { + slotObj = node->slotObj; + node->isSlotObject = false; + } + node = node->next; if (needToUnlock) m->unlock(); + + if (slotObj) { + locker.unlock(); + slotObj->destroyIfLastRef(); + locker.relock(); + } + sender->disconnectNotify(QMetaObjectPrivate::signal(sender->metaObject(), signal_index)); } } @@ -4273,6 +4287,43 @@ void qDeleteInEventHandler(QObject *o) */ /*! + \fn QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) + + \threadsafe + \overload connect() + + \since 5.2 + + Creates a connection of a given \a type from \a signal in + \a sender object to \a functor to be placed in a specific event + loop of \a context, and returns a handle to the connection + + The signal must be a function declared as a signal in the header. + The slot function can be any function or functor that can be connected + to the signal. + A function can be connected to a given signal if the signal as at + least as many argument as the slot. A functor can be connected to a signal + if they have exactly the same number of arguments. There must exist implicit + conversion between the types of the corresponding arguments in the + signal and the slot. + + Example: + + \snippet code/src_corelib_kernel_qobject.cpp 50 + + If your compiler support C++11 lambda expressions, you can use them: + + \snippet code/src_corelib_kernel_qobject.cpp 51 + + The connection will automatically disconnect if the sender or the context + is destroyed. + + \note If the compiler does not support C++11 variadic templates, the number + of arguments in the signal or slot are limited to 6, and the functor object + must not have an overloaded or templated operator(). + */ + +/*! \internal Implementation of the template version of connect |