diff options
Diffstat (limited to 'src/corelib/kernel/qobject.cpp')
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 79 |
1 files changed, 60 insertions, 19 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 36db55d25a..1312e64d8b 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -927,7 +927,7 @@ QObjectPrivate::Connection::~Connection() \relates QObject Returns the given \a object cast to type T if the object is of type - T (or of a subclass); otherwise returns 0. If \a object is 0 then + T (or of a subclass); otherwise returns 0. If \a object is 0 then it will also return 0. The class T must inherit (directly or indirectly) QObject and be @@ -1690,7 +1690,7 @@ void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re, } #endif // QT_NO_REGEXP -#ifndef QT_NO_REGEXP +#ifndef QT_NO_REGULAREXPRESSION /*! \internal */ @@ -1712,7 +1712,7 @@ void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re qt_qFindChildren_helper(obj, re, mo, list, options); } } -#endif // QT_NO_REGEXP +#endif // QT_NO_REGULAREXPRESSION /*! \internal @@ -3089,8 +3089,8 @@ bool QMetaObject::disconnect(const QObject *sender, int signal_index, /*! \internal -Disconnect a single signal connection. If QMetaObject::connect() has been called -multiple times for the same sender, signal_index, receiver and method_index only +Disconnect a single signal connection. If QMetaObject::connect() has been called +multiple times for the same sender, signal_index, receiver and method_index only one of these connections will be removed. */ bool QMetaObject::disconnectOne(const QObject *sender, int signal_index, @@ -3219,6 +3219,9 @@ bool QMetaObjectPrivate::disconnect(const QObject *sender, \snippet code/src_corelib_kernel_qobject.cpp 34 + If \a object itself has a properly set object name, its own signals are also + connected to its respective slots. + \sa QObject::setObjectName() */ void QMetaObject::connectSlotsByName(QObject *o) @@ -3227,39 +3230,66 @@ void QMetaObject::connectSlotsByName(QObject *o) return; const QMetaObject *mo = o->metaObject(); Q_ASSERT(mo); - const QObjectList list = o->findChildren<QObject *>(QString()); + const QObjectList list = // list of all objects to look for matching signals including... + o->findChildren<QObject *>(QString()) // all children of 'o'... + << o; // and the object 'o' itself + + // for each method/slot of o ... for (int i = 0; i < mo->methodCount(); ++i) { - QByteArray slotSignature = mo->method(i).methodSignature(); + const QByteArray slotSignature = mo->method(i).methodSignature(); const char *slot = slotSignature.constData(); Q_ASSERT(slot); + + // ...that starts with "on_", ... if (slot[0] != 'o' || slot[1] != 'n' || slot[2] != '_') continue; + + // ...we check each object in our list, ... bool foundIt = false; for(int j = 0; j < list.count(); ++j) { const QObject *co = list.at(j); - QByteArray objName = co->objectName().toLatin1(); - int len = objName.length(); - if (!len || qstrncmp(slot + 3, objName.data(), len) || slot[len+3] != '_') + const QByteArray coName = co->objectName().toLatin1(); + + // ...discarding those whose objectName is not fitting the pattern "on_<objectName>_...", ... + if (coName.isEmpty() || qstrncmp(slot + 3, coName.constData(), coName.size()) || slot[coName.size()+3] != '_') continue; + + const char *signal = slot + coName.size() + 4; // the 'signal' part of the slot name + + // ...for the presence of a matching signal "on_<objectName>_<signal>". const QMetaObject *smeta; - int sigIndex = co->d_func()->signalIndex(slot + len + 4, &smeta); - if (sigIndex < 0) { // search for compatible signals + int sigIndex = co->d_func()->signalIndex(signal, &smeta); + if (sigIndex < 0) { + // if no exactly fitting signal (name + complete parameter type list) could be found + // look for just any signal with the correct name and at least the slot's parameter list. + // Note: if more than one of thoses signals exist, the one that gets connected is + // chosen 'at random' (order of declaration in source file) + QList<QByteArray> compatibleSignals; const QMetaObject *smo = co->metaObject(); - int slotlen = qstrlen(slot + len + 4) - 1; - for (int k = 0; k < QMetaObjectPrivate::absoluteSignalCount(smo); ++k) { - QMetaMethod method = QMetaObjectPrivate::signal(smo, k); - if (!qstrncmp(method.methodSignature().constData(), slot + len + 4, slotlen)) { + int sigLen = qstrlen(signal) - 1; // ignore the trailing ')' + for (int k = QMetaObjectPrivate::absoluteSignalCount(smo)-1; k >= 0; --k) { + const QMetaMethod method = QMetaObjectPrivate::signal(smo, k); + if (!qstrncmp(method.methodSignature().constData(), signal, sigLen)) { smeta = method.enclosingMetaObject(); sigIndex = k; - break; + compatibleSignals.prepend(method.methodSignature()); } } + if (compatibleSignals.size() > 1) + qWarning() << "QMetaObject::connectSlotsByName: Connecting slot" << slot + << "with the first of the following compatible signals:" << compatibleSignals; } + if (sigIndex < 0) continue; + // we connect it... if (Connection(QMetaObjectPrivate::connect(co, sigIndex, smeta, o, i))) { foundIt = true; + // ...and stop looking for further objects with the same name. + // Note: the Designer will make sure each object name is unique in the above + // 'list' but other code may create two child objects with the same name. In + // this case one is chosen 'at random'. break; } } @@ -3268,7 +3298,11 @@ void QMetaObject::connectSlotsByName(QObject *o) while (mo->method(i + 1).attributes() & QMetaMethod::Cloned) ++i; } else if (!(mo->method(i).attributes() & QMetaMethod::Cloned)) { - qWarning("QMetaObject::connectSlotsByName: No matching signal for %s", slot); + // check if the slot has the following signature: "on_..._...(..." + int iParen = slotSignature.indexOf('('); + int iLastUnderscore = slotSignature.lastIndexOf('_', iParen-1); + if (iLastUnderscore > 3) + qWarning("QMetaObject::connectSlotsByName: No matching signal for %s", slot); } } } @@ -3334,7 +3368,7 @@ void QMetaObject::activate(QObject *sender, int signalOffset, int local_signal_i return; if (sender->d_func()->declarativeData && QAbstractDeclarativeData::signalEmitted) - QAbstractDeclarativeData::signalEmitted(sender->d_func()->declarativeData, sender, + QAbstractDeclarativeData::signalEmitted(sender->d_func()->declarativeData, sender, signal_index, argv); void *empty_argv[] = { 0 }; @@ -4151,6 +4185,9 @@ void qDeleteInEventHandler(QObject *o) \snippet code/src_corelib_kernel_qobject.cpp 25 make sure to declare the argument type with Q_DECLARE_METATYPE + + \note The number of arguments in the signal or slot are limited to 6 if + the compiler does not support C++11 variadic templates. */ @@ -4181,6 +4218,10 @@ void qDeleteInEventHandler(QObject *o) \snippet code/src_corelib_kernel_qobject.cpp 46 The connection will automatically disconnect if the sender 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(). */ /** |