diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp index 1f3789c6354..3e9f78c0ff9 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp @@ -366,7 +366,7 @@ QPushButton *button = parentWidget->findChild("button1", Qt::Find //! [42] -QListWidget *list = parentWidget->findChild(QString(), Qt::FindDirectChildrenOnly); +QListWidget *list = parentWidget->findChild(Qt::FindDirectChildrenOnly); //! [42] diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 1319402839e..fd58f33bf4a 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2022,6 +2022,24 @@ void QObject::killTimer(int id) \sa findChildren() */ +/*! + \fn template 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 QList QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options) const diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 3660a5b3c4e..5180b2335a3 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -140,7 +140,7 @@ public: void killTimer(int id); template - 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::type>::type ObjType; return static_cast(qt_qFindChild_helper(this, aName, ObjType::staticMetaObject, options)); @@ -156,6 +156,12 @@ public: return list; } + template + T findChild(Qt::FindChildOptions options = Qt::FindChildrenRecursively) const + { + return findChild({}, options); + } + template QList findChildren(Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 33ecb869b35..a06238a494b 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -510,6 +510,12 @@ void tst_QObject::qobject_castTemplate() QVERIFY(!::qobject_cast(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(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(Qt::FindDirectChildrenOnly); + QCOMPARE(op, &o1); + op = o.findChild(Qt::FindDirectChildrenOnly); + QCOMPARE(op, &t1); + op = o.findChild(Qt::FindDirectChildrenOnly); + QCOMPARE(op, nullptr); + op = o.findChild(Qt::FindChildrenRecursively); + QCOMPARE(op, &dr1); op = o.findChild(QString(), Qt::FindDirectChildrenOnly); QCOMPARE(op, &o1); op = o.findChild("", Qt::FindDirectChildrenOnly);