From cb32450c47e6bd6169c9f514a2e950729f82756f Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 13 Mar 2012 05:30:17 +0000 Subject: QRegularExpression: add QObject::findChildren overload This actually involved tiding up QObject sources a little bit to clearly separate QString / QRegExp overloads of findChildren. The corresponding qFindChildren overload for MSVC 6 compatibiltiy was *not* added. Change-Id: I84826b3df9275a9bda03608a5b66756890eda6f8 Reviewed-by: Lars Knoll --- src/corelib/kernel/qobject.cpp | 76 ++++++++++++++++++++--- src/corelib/kernel/qobject.h | 28 ++++++++- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 21 +++++++ 3 files changed, 112 insertions(+), 13 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 914441f7a8..a2f18ee4c9 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -50,6 +50,7 @@ #include "qvariant.h" #include "qmetaobject.h" #include +#include #include #include #include @@ -1558,7 +1559,21 @@ void QObject::killTimer(int id) Returns the children of this object that can be cast to type T and that have names matching the regular expression \a regExp, or an empty list if there are no such objects. - The search is performed recursively. + The search is performed recursively, unless \a options specifies the + option FindDirectChildrenOnly. +*/ + +/*! + \fn QList QObject::findChildren(const QRegularExpression &re, Qt::FindChildOptions options) const + \overload findChildren() + + \since 5.0 + + Returns the children of this object that can be cast to type T + and that have names matching the regular expression \a re, + or an empty list if there are no such objects. + The search is performed recursively, unless \a options specifies the + option FindDirectChildrenOnly. */ /*! @@ -1611,7 +1626,7 @@ void QObject::killTimer(int id) /*! \internal */ -void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re, +void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QMetaObject &mo, QList *list, Qt::FindChildOptions options) { if (!parent || !list) @@ -1621,18 +1636,59 @@ void qt_qFindChildren_helper(const QObject *parent, const QString &name, const Q for (int i = 0; i < children.size(); ++i) { obj = children.at(i); if (mo.cast(obj)) { - if (re) { - if (re->indexIn(obj->objectName()) != -1) - list->append(obj); - } else { - if (name.isNull() || obj->objectName() == name) - list->append(obj); - } + if (name.isNull() || obj->objectName() == name) + list->append(obj); + } + if (options & Qt::FindChildrenRecursively) + qt_qFindChildren_helper(obj, name, mo, list, options); + } +} + +#ifndef QT_NO_REGEXP +/*! + \internal +*/ +void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re, + const QMetaObject &mo, QList *list, Qt::FindChildOptions options) +{ + if (!parent || !list) + return; + const QObjectList &children = parent->children(); + QObject *obj; + for (int i = 0; i < children.size(); ++i) { + obj = children.at(i); + if (mo.cast(obj) && re.indexIn(obj->objectName()) != -1) + list->append(obj); + + if (options & Qt::FindChildrenRecursively) + qt_qFindChildren_helper(obj, re, mo, list, options); + } +} +#endif // QT_NO_REGEXP + +#ifndef QT_NO_REGEXP +/*! + \internal +*/ +void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re, + const QMetaObject &mo, QList *list, Qt::FindChildOptions options) +{ + if (!parent || !list) + return; + const QObjectList &children = parent->children(); + QObject *obj; + for (int i = 0; i < children.size(); ++i) { + obj = children.at(i); + if (mo.cast(obj)) { + QRegularExpressionMatch m = re.match(obj->objectName()); + if (m.hasMatch()) + list->append(obj); } if (options & Qt::FindChildrenRecursively) - qt_qFindChildren_helper(obj, name, re, mo, list, options); + qt_qFindChildren_helper(obj, re, mo, list, options); } } +#endif // QT_NO_REGEXP /*! \internal */ diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 9f09617071..37057bea50 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -73,13 +73,20 @@ class QWidget; #ifndef QT_NO_REGEXP class QRegExp; #endif +#ifndef QT_NO_REGEXP +class QRegularExpression; +#endif #ifndef QT_NO_USERDATA class QObjectUserData; #endif typedef QList QObjectList; -Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re, +Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, + const QMetaObject &mo, QList *list, Qt::FindChildOptions options); +Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re, + const QMetaObject &mo, QList *list, Qt::FindChildOptions options); +Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re, const QMetaObject &mo, QList *list, Qt::FindChildOptions options); Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options); @@ -163,7 +170,7 @@ public: QList *voidList; } u; u.typedList = &list; - qt_qFindChildren_helper(this, aName, 0, reinterpret_cast(0)->staticMetaObject, u.voidList, options); + qt_qFindChildren_helper(this, aName, reinterpret_cast(0)->staticMetaObject, u.voidList, options); return list; } @@ -177,7 +184,22 @@ public: QList *voidList; } u; u.typedList = &list; - qt_qFindChildren_helper(this, QString(), &re, reinterpret_cast(0)->staticMetaObject, u.voidList, options); + qt_qFindChildren_helper(this, re, reinterpret_cast(0)->staticMetaObject, u.voidList, options); + return list; + } +#endif + +#ifndef QT_NO_REGEXP + template + inline QList findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const + { + QList list; + union { + QList *typedList; + QList *voidList; + } u; + u.typedList = &list; + qt_qFindChildren_helper(this, re, reinterpret_cast(0)->staticMetaObject, u.voidList, options); return list; } #endif diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index c6667ff2a8..c2ded70d80 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -656,6 +657,26 @@ void tst_QObject::findChildren() l = qFindChildren(&o, QRegExp("harry")); QCOMPARE(l.size(), 0); + l = o.findChildren(QRegularExpression("o.*")); + QCOMPARE(l.size(), 5); + QVERIFY(l.contains(&o1)); + QVERIFY(l.contains(&o2)); + QVERIFY(l.contains(&o11)); + QVERIFY(l.contains(&o12)); + QVERIFY(l.contains(&o111)); + l = o.findChildren(QRegularExpression("t.*")); + QCOMPARE(l.size(), 2); + QVERIFY(l.contains(&t1)); + QVERIFY(l.contains(&t121)); + tl = o.findChildren(QRegularExpression(".*")); + QCOMPARE(tl.size(), 3); + QVERIFY(tl.contains(&t1)); + QVERIFY(tl.contains(&t121)); + tl = o.findChildren(QRegularExpression("o.*")); + QCOMPARE(tl.size(), 0); + l = o.findChildren(QRegularExpression("harry")); + QCOMPARE(l.size(), 0); + // empty and null string check op = qFindChild(&o); QCOMPARE(op, &o1); -- cgit v1.2.3