L
L
LegoGo2016-05-18 19:28:12
Qt
LegoGo, 2016-05-18 19:28:12

Why doesn't parent->findchild("") find objects created by Repeater?

If you create a qml object like this:
ToggleButton
{ objectname: "name_1"
text: "text"
}
Then when you call Object* obj1= this->parent()->findChild*>("name_1"); A non-empty obj1 is created and its properties can be changed.
If you create a qml object as a Repeater element:
Repeater
{ model: Model
ToggleButton
{ objectName: "name_"+index
text: model.modelData.text
}
}
In this case, qml objects are displayed, but through Object* obj1= this-> parent()->findChild*>("name_1"); I get obj1 = 0x0. Although - console output from qml - (console.log("objectName:",objectName)) shows that objectName is set.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava_d2000, 2016-06-18
@Slava_d2000

I just started to study qml, I can make a mistake in the answer. In my opinion, the problem is the following, by definition, the index starts from 0, not from 1, and the number of repetitions is set in the model parameter, in your case, what is Model ? Is it some sort of number or something? As a result of executing the code, you have a single instance of ToggleButton with objectName = "name_0".

M
Maxim Rybas, 2016-08-23
@MaxQwerty

You can use this example to get a full dump of the property objects in the QML object tree:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QMetaProperty>
#include <QList>

#include <QDebug>

void dumpQmlItemProperties(QObject* qmlItem)
{
    const QMetaObject* metaObj = qmlItem->metaObject();
    qDebug() << metaObj->className();
    for(int i=0; i<metaObj->propertyCount(); ++i)
    {
        QMetaProperty prop = metaObj->property(i);
        qDebug()
                 << prop.name()
                 << prop.read(qmlItem);
    }
}

void dumpQmlObjectsTree(QList<QObject*> qmlItems)
{
    QList<QObject*>::iterator it = qmlItems.begin();
    while(it != qmlItems.end())
    {
        qDebug() << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
        dumpQmlItemProperties(*it);
        qDebug() << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
        dumpQmlObjectsTree((*it)->children());
        ++it;
    }
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    dumpQmlObjectsTree(engine.rootObjects());
    return app.exec();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question