Finding child objects by their type

Last edited on

Synopsis

This example shows how to get a list of child objects of a specific type. The getChildrenOfType() function can search for all such child objects, or search only to a limited depth (default depth is 1000).

Python:

def main():
    startApplication(app)

    # ...

    widget = waitForObject(names.some_object_name)

    # Find only immediate children of type LineEdit:
    list1 = getChildrenOfType(widget, "LineEdit", 0)

    # Find children of type LineEdit up to a depth of 2:
    list2 = getChildrenOfType(widget, "LineEdit", 2)

    # Find all children of type LineEdit:
    listAll = getChildrenOfType(widget, "LineEdit")

def getChildrenOfType(parent, typename, depth=1000):
    children = []
    for child in object.children(parent):
        if className(child) == typename:
            children.append(child)
        if depth:
            children.extend(getChildrenOfType(child, typename, depth - 1))
    return children
test.py

JavaScript:

function main()
{
    startApplication(app);

    // ...

    widget = waitForObject(names.some_object_name)

    // Find only immediate children of type LineEdit:
    list1 = getChildrenOfType(widget, "LineEdit", 0);

    // Find children of type LineEdit up to a depth of 2:
    list2 = getChildrenOfType(widget, "LineEdit", 2);

    // Find all children of type LineEdit:
    listAll = getChildrenOfType(widget, "LineEdit");
}

function getChildrenOfType(parent, typeNameStr, depth)
{
    if (depth == null) {
        depth = 1000;
    }

    var children = [];
    var allChildren = object.children(parent);
    for (var i = 0; i < allChildren.length; i++) {
        var child = allChildren[i];
        if (typeName(child) == typeNameStr) {
            children.push(child);
        }
        if (depth > 0) {
            var subChildren = getChildrenOfType(child, typeNameStr, depth - 1)
            for (var j = 0; j < subChildren.length; j++) {
                children.push(subChildren[j]);
            }
        }
    }
    return children;
}
test.js