QObject: allow calling findChild() without a name
Add an overload of findChild() without a name argument to be able to call the function with options value only. [ChangeLog][QtCore][QObject] Added findChild() overload taking no name. Task-number: QTBUG-103986 Change-Id: Id06b6041408fcf4cc1eeba975afce03f3a28f858 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
parent
1126a590f9
commit
bf946e8e3b
@ -366,7 +366,7 @@ QPushButton *button = parentWidget->findChild<QPushButton *>("button1", Qt::Find
|
||||
|
||||
|
||||
//! [42]
|
||||
QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDirectChildrenOnly);
|
||||
QListWidget *list = parentWidget->findChild<QListWidget *>(Qt::FindDirectChildrenOnly);
|
||||
//! [42]
|
||||
|
||||
|
||||
|
@ -2022,6 +2022,24 @@ void QObject::killTimer(int id)
|
||||
\sa findChildren()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn template<typename T> T *QObject::findChild(Qt::FindChildOptions options) const
|
||||
\overload
|
||||
\since 6.7
|
||||
|
||||
Returns the child of this object that can be cast into type T, or
|
||||
\nullptr if there is no such object.
|
||||
The search is performed recursively, unless \a options specifies the
|
||||
option FindDirectChildrenOnly.
|
||||
|
||||
If there is more than one child matching the search, the most-direct ancestor
|
||||
is returned. If there are several most-direct ancestors, the first child in
|
||||
children() will be returned. In that case, it's better to use findChildren()
|
||||
to get the complete list of all children.
|
||||
|
||||
\sa findChildren()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn template<typename T> QList<T> QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options) const
|
||||
|
||||
|
@ -140,7 +140,7 @@ public:
|
||||
void killTimer(int id);
|
||||
|
||||
template<typename T>
|
||||
T findChild(QAnyStringView aName = {}, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
|
||||
T findChild(QAnyStringView aName, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
|
||||
{
|
||||
typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
|
||||
return static_cast<T>(qt_qFindChild_helper(this, aName, ObjType::staticMetaObject, options));
|
||||
@ -156,6 +156,12 @@ public:
|
||||
return list;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T findChild(Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
|
||||
{
|
||||
return findChild<T>({}, options);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QList<T> findChildren(Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
|
||||
{
|
||||
|
@ -510,6 +510,12 @@ void tst_QObject::qobject_castTemplate()
|
||||
QVERIFY(!::qobject_cast<ReceiverObject*>(o.data()));
|
||||
}
|
||||
|
||||
class DerivedObj : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using QObject::QObject;
|
||||
};
|
||||
|
||||
void tst_QObject::findChildren()
|
||||
{
|
||||
QObject o;
|
||||
@ -769,7 +775,20 @@ void tst_QObject::findChildren()
|
||||
l = o.findChildren<QObject*>(QRegularExpression("^harry$"), Qt::FindDirectChildrenOnly);
|
||||
QCOMPARE(l.size(), 0);
|
||||
|
||||
DerivedObj dr1(&o111);
|
||||
DerivedObj dr2(&o111);
|
||||
Q_SET_OBJECT_NAME(dr1);
|
||||
Q_SET_OBJECT_NAME(dr2);
|
||||
|
||||
// empty and null string check
|
||||
op = o.findChild<QObject*>(Qt::FindDirectChildrenOnly);
|
||||
QCOMPARE(op, &o1);
|
||||
op = o.findChild<QTimer*>(Qt::FindDirectChildrenOnly);
|
||||
QCOMPARE(op, &t1);
|
||||
op = o.findChild<DerivedObj*>(Qt::FindDirectChildrenOnly);
|
||||
QCOMPARE(op, nullptr);
|
||||
op = o.findChild<DerivedObj*>(Qt::FindChildrenRecursively);
|
||||
QCOMPARE(op, &dr1);
|
||||
op = o.findChild<QObject*>(QString(), Qt::FindDirectChildrenOnly);
|
||||
QCOMPARE(op, &o1);
|
||||
op = o.findChild<QObject*>("", Qt::FindDirectChildrenOnly);
|
||||
|
Loading…
Reference in New Issue
Block a user