Answer the question
In order to leave comments, you need to log in
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
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".
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 questionAsk a Question
731 491 924 answers to any question