summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-03-22 18:08:17 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2011-03-23 13:36:17 -0300
commit6f8f72824181d469b0b855790a29ce56ce38e995 (patch)
tree787c63f42ede2a6eb617c3577479edff558d24d6
parentf645a79f0397a92ef52ff42524752339fc213825 (diff)
downloadpyside-6f8f72824181d469b0b855790a29ce56ce38e995.tar.gz
pyside-6f8f72824181d469b0b855790a29ce56ce38e995.tar.xz
pyside-6f8f72824181d469b0b855790a29ce56ce38e995.zip
Fix bug 685 - "The second arg. of QObject.findChildren doesn't have a default value and doesn't support regexes."
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--PySide/QtCore/glue/qobject_findchild.cpp21
-rw-r--r--PySide/QtCore/typesystem_core.xml12
-rw-r--r--tests/QtCore/qobject_parent_test.py11
3 files changed, 37 insertions, 7 deletions
diff --git a/PySide/QtCore/glue/qobject_findchild.cpp b/PySide/QtCore/glue/qobject_findchild.cpp
index 351aa69..669beac 100644
--- a/PySide/QtCore/glue/qobject_findchild.cpp
+++ b/PySide/QtCore/glue/qobject_findchild.cpp
@@ -17,14 +17,23 @@ static QObject* _findChildHelper(const QObject* parent, const QString& name, PyT
return 0;
}
-static void _findChildrenHelper(const QObject* parent, const QString& name, PyTypeObject* desiredType, PyObject* result)
+static inline bool _findChildrenComparator(const QObject*& child, const QRegExp& name)
{
- foreach(QObject* child, parent->children()) {
+ return name.indexIn(child->objectName()) != -1;
+}
+
+static inline bool _findChildrenComparator(const QObject*& child, const QString& name)
+{
+ return name.isNull() || name == child->objectName();
+}
+
+template<typename T>
+static void _findChildrenHelper(const QObject* parent, const T& name, PyTypeObject* desiredType, PyObject* result)
+{
+ foreach(const QObject* child, parent->children()) {
Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject*](child));
- if (PyType_IsSubtype(pyChild->ob_type, desiredType)
- && (name.isNull() || name == child->objectName())) {
+ if (PyType_IsSubtype(pyChild->ob_type, desiredType) && _findChildrenComparator(child, name))
PyList_Append(result, pyChild);
- }
_findChildrenHelper(child, name, desiredType, result);
}
-} \ No newline at end of file
+}
diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml
index 6b3721d..38989b9 100644
--- a/PySide/QtCore/typesystem_core.xml
+++ b/PySide/QtCore/typesystem_core.xml
@@ -1325,6 +1325,18 @@
<modify-argument index="return">
<parent index="this" action="add"/>
</modify-argument>
+ <modify-argument index="2">
+ <replace-default-expression with="QString()" />
+ </modify-argument>
+ </add-function>
+ <add-function signature="findChildren(PyTypeObject*, const QRegExp&amp;)" return-type="PySequence*" >
+ <inject-code class="target" position="beginning">
+ %PYARG_0 = PyList_New(0);
+ _findChildrenHelper(%CPPSELF, %2, (PyTypeObject*)%PYARG_1, %PYARG_0);
+ </inject-code>
+ <modify-argument index="return">
+ <parent index="this" action="add"/>
+ </modify-argument>
</add-function>
<add-function signature="tr(const char *, const char *, int)" return-type="QString">
diff --git a/tests/QtCore/qobject_parent_test.py b/tests/QtCore/qobject_parent_test.py
index df81216..cda6022 100644
--- a/tests/QtCore/qobject_parent_test.py
+++ b/tests/QtCore/qobject_parent_test.py
@@ -4,7 +4,7 @@
import unittest
from sys import getrefcount
-from PySide.QtCore import QObject, QTimer
+from PySide.QtCore import *
class ParentRefCountCase(unittest.TestCase):
'''Test case for the refcount changes of setParent'''
@@ -108,6 +108,15 @@ class ParentCase(unittest.TestCase):
self.assertEqual(sorted(target_children), sorted(test_children))
+ # test findChildren default value
+ res = parent.findChildren(QTimer)
+ self.assertEqual(len(res), 20)
+
+ # test findChildre with a regex
+ res = parent.findChildren(QObject, QRegExp("^fo+"))
+ self.assertEqual(sorted(res), sorted(test_children))
+
+
def testParentEquality(self):
#QObject.parent() == parent
parent = QObject()