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 <lars.knoll@nokia.com>
This commit is contained in:
parent
8659495202
commit
cb32450c47
@ -50,6 +50,7 @@
|
|||||||
#include "qvariant.h"
|
#include "qvariant.h"
|
||||||
#include "qmetaobject.h"
|
#include "qmetaobject.h"
|
||||||
#include <qregexp.h>
|
#include <qregexp.h>
|
||||||
|
#include <qregularexpression.h>
|
||||||
#include <qthread.h>
|
#include <qthread.h>
|
||||||
#include <private/qthread_p.h>
|
#include <private/qthread_p.h>
|
||||||
#include <qdebug.h>
|
#include <qdebug.h>
|
||||||
@ -1558,7 +1559,21 @@ void QObject::killTimer(int id)
|
|||||||
Returns the children of this object that can be cast to type T
|
Returns the children of this object that can be cast to type T
|
||||||
and that have names matching the regular expression \a regExp,
|
and that have names matching the regular expression \a regExp,
|
||||||
or an empty list if there are no such objects.
|
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<T> 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
|
\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<void*> *list, Qt::FindChildOptions options)
|
const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
|
||||||
{
|
{
|
||||||
if (!parent || !list)
|
if (!parent || !list)
|
||||||
@ -1621,19 +1636,60 @@ void qt_qFindChildren_helper(const QObject *parent, const QString &name, const Q
|
|||||||
for (int i = 0; i < children.size(); ++i) {
|
for (int i = 0; i < children.size(); ++i) {
|
||||||
obj = children.at(i);
|
obj = children.at(i);
|
||||||
if (mo.cast(obj)) {
|
if (mo.cast(obj)) {
|
||||||
if (re) {
|
if (name.isNull() || obj->objectName() == name)
|
||||||
if (re->indexIn(obj->objectName()) != -1)
|
list->append(obj);
|
||||||
list->append(obj);
|
|
||||||
} else {
|
|
||||||
if (name.isNull() || obj->objectName() == name)
|
|
||||||
list->append(obj);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (options & Qt::FindChildrenRecursively)
|
if (options & Qt::FindChildrenRecursively)
|
||||||
qt_qFindChildren_helper(obj, name, re, mo, list, options);
|
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<void*> *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<void*> *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, re, mo, list, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
|
||||||
/*! \internal
|
/*! \internal
|
||||||
*/
|
*/
|
||||||
QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options)
|
QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options)
|
||||||
|
@ -73,13 +73,20 @@ class QWidget;
|
|||||||
#ifndef QT_NO_REGEXP
|
#ifndef QT_NO_REGEXP
|
||||||
class QRegExp;
|
class QRegExp;
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
class QRegularExpression;
|
||||||
|
#endif
|
||||||
#ifndef QT_NO_USERDATA
|
#ifndef QT_NO_USERDATA
|
||||||
class QObjectUserData;
|
class QObjectUserData;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef QList<QObject*> QObjectList;
|
typedef QList<QObject*> 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<void *> *list, Qt::FindChildOptions options);
|
||||||
|
Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re,
|
||||||
|
const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
|
||||||
|
Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re,
|
||||||
const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
|
const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
|
||||||
Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, 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<void *> *voidList;
|
QList<void *> *voidList;
|
||||||
} u;
|
} u;
|
||||||
u.typedList = &list;
|
u.typedList = &list;
|
||||||
qt_qFindChildren_helper(this, aName, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
|
qt_qFindChildren_helper(this, aName, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +184,22 @@ public:
|
|||||||
QList<void *> *voidList;
|
QList<void *> *voidList;
|
||||||
} u;
|
} u;
|
||||||
u.typedList = &list;
|
u.typedList = &list;
|
||||||
qt_qFindChildren_helper(this, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
|
qt_qFindChildren_helper(this, re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
template<typename T>
|
||||||
|
inline QList<T> findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
|
||||||
|
{
|
||||||
|
QList<T> list;
|
||||||
|
union {
|
||||||
|
QList<T> *typedList;
|
||||||
|
QList<void *> *voidList;
|
||||||
|
} u;
|
||||||
|
u.typedList = &list;
|
||||||
|
qt_qFindChildren_helper(this, re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
#include <qpointer.h>
|
#include <qpointer.h>
|
||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
#include <qregexp.h>
|
#include <qregexp.h>
|
||||||
|
#include <qregularexpression.h>
|
||||||
#include <qmetaobject.h>
|
#include <qmetaobject.h>
|
||||||
#include <qvariant.h>
|
#include <qvariant.h>
|
||||||
#include <QTcpServer>
|
#include <QTcpServer>
|
||||||
@ -656,6 +657,26 @@ void tst_QObject::findChildren()
|
|||||||
l = qFindChildren<QObject*>(&o, QRegExp("harry"));
|
l = qFindChildren<QObject*>(&o, QRegExp("harry"));
|
||||||
QCOMPARE(l.size(), 0);
|
QCOMPARE(l.size(), 0);
|
||||||
|
|
||||||
|
l = o.findChildren<QObject*>(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<QObject*>(QRegularExpression("t.*"));
|
||||||
|
QCOMPARE(l.size(), 2);
|
||||||
|
QVERIFY(l.contains(&t1));
|
||||||
|
QVERIFY(l.contains(&t121));
|
||||||
|
tl = o.findChildren<QTimer*>(QRegularExpression(".*"));
|
||||||
|
QCOMPARE(tl.size(), 3);
|
||||||
|
QVERIFY(tl.contains(&t1));
|
||||||
|
QVERIFY(tl.contains(&t121));
|
||||||
|
tl = o.findChildren<QTimer*>(QRegularExpression("o.*"));
|
||||||
|
QCOMPARE(tl.size(), 0);
|
||||||
|
l = o.findChildren<QObject*>(QRegularExpression("harry"));
|
||||||
|
QCOMPARE(l.size(), 0);
|
||||||
|
|
||||||
// empty and null string check
|
// empty and null string check
|
||||||
op = qFindChild<QObject*>(&o);
|
op = qFindChild<QObject*>(&o);
|
||||||
QCOMPARE(op, &o1);
|
QCOMPARE(op, &o1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user