C ++ access to ListElement (QML)

I am trying to access ListElement (QML) properties: color;

ListModel { id:myListModel ListElement {name:"one" ;color:"red";objectName:"first"} ListElement(name:"two";color:"green"} } 

in C ++, I should use:

 QObject* o=ui->declarativeView->rootObject()->findChild<QObject*>("first"); o->setProperty("color","blue"); 

But I can’t access these properties. Please, help.

+4
source share
1 answer

A ListModel not parsed as typical QML, but uses a custom parser to process ListElement declarations. This allows the model to avoid creating expensive objects for each data item. ListModel has a number of functions available for managing model data through QML. You can add your own functions to the model in QML, which you can access with C ++, for example.

 ListModel { id:myListModel objectName: "model" function setColor(index, color) { myListModel.setProperty(index, "color", color) } ListElement {name:"one";color:"red"} ListElement {name:"two";color:"green"} } 

and in C ++:

 QObject* o=ui->declarativeView->rootObject()->findChild<QObject*>("model"); QMetaObject::invokeMethod(o, "setColor", Q_ARG(QVariant, 0), Q_ARG(QVariant, "yellow")); 
+4
source

Source: https://habr.com/ru/post/1438469/


All Articles