diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-06-14 14:20:06 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-06-14 20:52:18 +0200 |
commit | c6b66ffab442d5ad9f0eb6a1d2dafec8bdded245 (patch) | |
tree | 13c4141683897fd6b26bdd4e261d008b1230db52 /sources | |
parent | d0c6c06df04ee23fe523b477e9fb2a508cf78f3e (diff) |
PySide6: Add Qt::FindChildOptions to QObject::findChild(ren)
Pick-to: 6.1
Fixes: PYSIDE-905
Change-Id: Iae343d15fb0db1d37f95ab2d443596777d2dfac6
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 9 | ||||
-rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 25 | ||||
-rw-r--r-- | sources/pyside6/tests/QtCore/qobject_parent_test.py | 28 |
3 files changed, 49 insertions, 13 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index 856c01224..f21c1d382 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -1703,7 +1703,8 @@ <inject-code class="native" file="../glue/qtcore.cpp" snippet="qobject-findchild-1"/> - <add-function signature="findChild(PyTypeObject*,const QString&)" return-type="PyObject*"> + <add-function signature="findChild(PyTypeObject*@type@,const QString&@name@,Qt::FindChildOptions@options@=Qt::FindChildrenRecursively)" + return-type="PyObject*"> <inject-documentation format="target" mode="append"> To find the child of a certain QObject, the first argument of this function should be the child's type, and the second the name of the child: @@ -1725,7 +1726,8 @@ <replace-default-expression with="QString()"/> </modify-argument> </add-function> - <add-function signature="findChildren(PyTypeObject*,const QString&)" return-type="PySequence*" > + <add-function signature="findChildren(PyTypeObject*@type@,const QString&@name@,Qt::FindChildOptions@options@=Qt::FindChildrenRecursively)" + return-type="PySequence*" > <inject-documentation format="target" mode="append"> Like the method *findChild*, the first parameter should be the child's type. </inject-documentation> @@ -1737,7 +1739,8 @@ <replace-default-expression with="QString()"/> </modify-argument> </add-function> - <add-function signature="findChildren(PyTypeObject*,const QRegularExpression&)" return-type="PySequence*" > + <add-function signature="findChildren(PyTypeObject*@type@,const QRegularExpression&@pattern@,Qt::FindChildOptions@options@=Qt::FindChildrenRecursively)" + return-type="PySequence*" > <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qobject-findchildren"/> <modify-argument index="return"> <parent index="this" action="add"/> diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index a105666a1..f92c063be 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -815,7 +815,9 @@ qRegisterMetaType<QList<int> >("QList<int>"); // @snippet qobject-metaobject // @snippet qobject-findchild-1 -static QObject *_findChildHelper(const QObject *parent, const QString &name, PyTypeObject *desiredType) +static QObject *_findChildHelper(const QObject *parent, const QString &name, + PyTypeObject *desiredType, + Qt::FindChildOptions options) { for (auto *child : parent->children()) { Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject *](child)); @@ -825,10 +827,12 @@ static QObject *_findChildHelper(const QObject *parent, const QString &name, PyT } } - for (auto *child : parent->children()) { - QObject *obj = _findChildHelper(child, name, desiredType); - if (obj) - return obj; + if (options.testFlag(Qt::FindChildrenRecursively)) { + for (auto *child : parent->children()) { + QObject *obj = _findChildHelper(child, name, desiredType, options); + if (obj) + return obj; + } } return nullptr; } @@ -844,25 +848,28 @@ static inline bool _findChildrenComparator(const QObject *&child, const QString } template<typename T> -static void _findChildrenHelper(const QObject *parent, const T& name, PyTypeObject *desiredType, PyObject *result) +static void _findChildrenHelper(const QObject *parent, const T& name, PyTypeObject *desiredType, + Qt::FindChildOptions options, + PyObject *result) { for (const auto *child : parent->children()) { Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject *](child)); if (PyType_IsSubtype(Py_TYPE(pyChild), desiredType) && _findChildrenComparator(child, name)) PyList_Append(result, pyChild); - _findChildrenHelper(child, name, desiredType, result); + if (options.testFlag(Qt::FindChildrenRecursively)) + _findChildrenHelper(child, name, desiredType, options, result); } } // @snippet qobject-findchild-1 // @snippet qobject-findchild-2 -QObject *child = _findChildHelper(%CPPSELF, %2, reinterpret_cast<PyTypeObject *>(%PYARG_1)); +QObject *child = _findChildHelper(%CPPSELF, %2, reinterpret_cast<PyTypeObject *>(%PYARG_1), %3); %PYARG_0 = %CONVERTTOPYTHON[QObject *](child); // @snippet qobject-findchild-2 // @snippet qobject-findchildren %PYARG_0 = PyList_New(0); -_findChildrenHelper(%CPPSELF, %2, reinterpret_cast<PyTypeObject *>(%PYARG_1), %PYARG_0); +_findChildrenHelper(%CPPSELF, %2, reinterpret_cast<PyTypeObject *>(%PYARG_1), %3, %PYARG_0); // @snippet qobject-findchildren // @snippet qobject-tr diff --git a/sources/pyside6/tests/QtCore/qobject_parent_test.py b/sources/pyside6/tests/QtCore/qobject_parent_test.py index 972d4f6fa..7e024fdde 100644 --- a/sources/pyside6/tests/QtCore/qobject_parent_test.py +++ b/sources/pyside6/tests/QtCore/qobject_parent_test.py @@ -38,7 +38,7 @@ sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) from init_paths import init_test_paths init_test_paths(False) -from PySide6.QtCore import QObject, QRegularExpression, QTimer +from PySide6.QtCore import QObject, QRegularExpression, QTimer, Qt class ParentRefCountCase(unittest.TestCase): @@ -113,6 +113,32 @@ class ParentCase(unittest.TestCase): for i, child in enumerate(children): self.assertEqual(child, parent.findChild(QObject, f'object{i}')) + + def testFindChildOptions(self): + parent = QObject() + child = QObject(parent) + nested_child_name = 'nestedChild' + nested_child = QObject(child) + nested_child.setObjectName(nested_child_name) + + search_result = parent.findChild(QObject, nested_child_name) + self.assertTrue(search_result) + search_result = parent.findChild(QObject, nested_child_name, + Qt.FindChildrenRecursively) + self.assertTrue(search_result) + search_result = parent.findChild(QObject, nested_child_name, + Qt.FindDirectChildrenOnly) + self.assertFalse(search_result) + + search_results = parent.findChildren(QObject, nested_child_name) + self.assertEqual(len(search_results), 1) + search_result = parent.findChildren(QObject, nested_child_name, + Qt.FindChildrenRecursively) + self.assertEqual(len(search_results), 1) + search_results = parent.findChildren(QObject, nested_child_name, + Qt.FindDirectChildrenOnly) + self.assertEqual(len(search_results), 0) + def testFindChildWithoutName(self): parent = QObject() children = [QObject(parent) for i in range(20)] |