Skip to content

Commit cdd8fb3

Browse files
committed
Fix findChild/findChildren logic:
Do not skip children of children whose name does not match. Do not bail out if a child is nullptr (not sure if this can occur). Use modern for loop.
1 parent b806123 commit cdd8fb3

2 files changed

Lines changed: 28 additions & 45 deletions

File tree

src/PythonQtStdDecorators.cpp

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,14 @@ QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* t
270270
return list;
271271
}
272272

273-
QObject* PythonQtStdDecorators::findChild(QObject* parent, const char* typeName, const QMetaObject* meta,
273+
QObject* PythonQtStdDecorators::findChild(const QObject* parent, const char* typeName, const QMetaObject* meta,
274274
const QString& name)
275275
{
276276
const QObjectList& children = parent->children();
277277

278-
int i;
279-
for (i = 0; i < children.size(); ++i) {
280-
QObject* obj = children.at(i);
281-
278+
for (QObject* obj : children) {
282279
if (!obj)
283-
return nullptr;
280+
continue;
284281

285282
// Skip if the name doesn't match.
286283
if (!name.isNull() && obj->objectName() != name)
@@ -290,69 +287,55 @@ QObject* PythonQtStdDecorators::findChild(QObject* parent, const char* typeName,
290287
return obj;
291288
}
292289

293-
for (i = 0; i < children.size(); ++i) {
294-
QObject* obj = findChild(children.at(i), typeName, meta, name);
290+
for (QObject* child : children) {
291+
if (child) {
292+
QObject* obj = findChild(child, typeName, meta, name);
295293

296-
if (obj != nullptr)
297-
return obj;
294+
if (obj != nullptr)
295+
return obj;
296+
}
298297
}
299298

300299
return nullptr;
301300
}
302301

303-
int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta,
302+
void PythonQtStdDecorators::findChildren(const QObject* parent, const char* typeName, const QMetaObject* meta,
304303
const QString& name, QList<QObject*>& list)
305304
{
306305
const QObjectList& children = parent->children();
307-
int i;
308-
309-
for (i = 0; i < children.size(); ++i) {
310-
QObject* obj = children.at(i);
311306

307+
for (QObject* obj : children) {
312308
if (!obj)
313-
return -1;
314-
315-
// Skip if the name doesn't match.
316-
if (!name.isNull() && obj->objectName() != name)
317309
continue;
318310

319-
if ((typeName && obj->inherits(typeName)) || (meta && meta->cast(obj))) {
320-
list += obj;
311+
if (name.isNull() || obj->objectName() == name) {
312+
if ((typeName && obj->inherits(typeName)) || (meta && meta->cast(obj))) {
313+
list += obj;
314+
}
321315
}
322316

323-
if (findChildren(obj, typeName, meta, name, list) < 0)
324-
return -1;
317+
findChildren(obj, typeName, meta, name, list);
325318
}
326-
327-
return 0;
328319
}
329320

330-
int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta,
321+
void PythonQtStdDecorators::findChildren(const QObject* parent, const char* typeName, const QMetaObject* meta,
331322
const QRegularExpression& regExp, QList<QObject*>& list)
332323
{
333324
const QObjectList& children = parent->children();
334-
int i;
335-
336-
for (i = 0; i < children.size(); ++i) {
337-
QObject* obj = children.at(i);
338325

326+
for (QObject* obj : children) {
339327
if (!obj)
340-
return -1;
341-
342-
// Skip if the name doesn't match.
343-
QRegularExpressionMatch match = regExp.match(obj->objectName());
344-
if (match.hasMatch() == false)
345328
continue;
346329

347-
if ((typeName && obj->inherits(typeName)) || (meta && meta->cast(obj))) {
348-
list += obj;
330+
QRegularExpressionMatch match = regExp.match(obj->objectName());
331+
if (match.hasMatch()) {
332+
if ((typeName && obj->inherits(typeName)) || (meta && meta->cast(obj))) {
333+
list += obj;
334+
}
349335
}
350336

351-
if (findChildren(obj, typeName, meta, regExp, list) < 0)
352-
return -1;
337+
findChildren(obj, typeName, meta, regExp, list);
353338
}
354-
355-
return 0;
356339
}
357340

358341
const QMetaObject* PythonQtStdDecorators::metaObject(QObject* obj)

src/PythonQtStdDecorators.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ public Q_SLOTS:
151151
void static_QTimer_singleShot(int msec, PyObject* callable);
152152

153153
private:
154-
QObject* findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name);
155-
int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name,
156-
QList<QObject*>& list);
157-
int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegularExpression& regExp,
154+
QObject* findChild(const QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name);
155+
void findChildren(const QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name,
158156
QList<QObject*>& list);
157+
void findChildren(const QObject* parent, const char* typeName, const QMetaObject* meta,
158+
const QRegularExpression& regExp, QList<QObject*>& list);
159159
};
160160

161161
class PythonQtSingleShotTimer : public QTimer

0 commit comments

Comments
 (0)