ymoreau
ymoreau

Reputation: 3996

Equivalent among qml-child-items of the QObject::findChild function

T QObject::findChild allows you to search through the children a specific object by class and by name :

QPushButton *button = parentWidget->findChild<QPushButton*>("button1",
                                                Qt::FindDirectChildrenOnly);

I need to find a child item like this, by class and name. Something like

MyQQuickItem *item = parentItem->findChildItem<MyQQuickItem*>("item1");

But the function does not exist for QQuickItem.
What's the most convenient/efficient way to achieve this ?

So far I can only think about a loop on the QQuickItem::childItems list.

Upvotes: 1

Views: 811

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

A similar logic should be implemented as shown below:

void FindChildren_helper(const QQuickItem *parent, const QString &name, const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
{
    if (!parent || !list)
        return;
    const QList<QQuickItem *> &children = parent->childItems();
    QQuickItem *obj;
    for (int i = 0; i < children.size(); ++i) {
        obj = children.at(i);
        if(mo.cast(obj)){
            if (name.isNull() || obj->objectName() == name)
                list->append(obj);
        }
        if (options & Qt::FindChildrenRecursively)
            FindChildren_helper(obj, name, mo, list, options);
    }
}

void FindChildren_helper(const QQuickItem *parent, const QRegularExpression &re, const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
{
    if (!parent || !list)
        return;
    const QList<QQuickItem *> &children = parent->childItems();
    QQuickItem *obj;
    for (int i = 0; i < children.size(); ++i) {
        obj = children.at(i);
        if(mo.cast(obj)){
            QRegularExpressionMatch m = re.match(obj->objectName());
            if (m.hasMatch())
                list->append(obj);
        }
        if (options & Qt::FindChildrenRecursively)
            FindChildren_helper(obj, re, mo, list, options);
    }
}

template<typename T> QList<T> findChildren(QQuickItem *parent, const QString &aName = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively)
{
    typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
    QList<T> list;
    FindChildren_helper(parent, aName, ObjType::staticMetaObject, reinterpret_cast<QList<void *> *>(&list), options);
    return list;
}


template<typename T> QList<T> findChildren(QQuickItem *parent, const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively)
{
    typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
    QList<T> list;
    FindChildren_helper(parent, re, ObjType::staticMetaObject, reinterpret_cast<QList<void *> *>(&list), options);
    return list;
}

Example:

QList<QQuickItem *> childs = findChildren<QQuickItem *>(parentItem, "somename", Qt::FindDirectChildrenOnly);

Upvotes: 1

Related Questions