Search code examples
qtqobject

Is there a simple way of limiting QObject::findChild() to direct children only?


Question is in the title. I can't find anything obvious in the documentation which suggests a way of doing this. Must I use the recursive children finding methods and test each child's parent pointer sequentially to filter out the non-direct children?

Incidentally, the documentation seems to refer to "direct ancestor"s by which I think it means "direct descendant".

(edit: I'm looking for simplicity, so the answer doesn't have to use the findChild() method.)


Solution

  • I have added a flag to QObject::findChild() in Qt 5, called Qt::FindDirectChildOnly, which allows you to do exactly this.

    So this issue will soon be a problem of the past :-)