How to access qml properties from a ListItemComponent

by Developer on ‎06-04-2013 03:47 PM - edited on ‎11-08-2013 01:22 PM by Retired (4,880 Views)

A common issue when using ListItemComponent is that it resides in a different context from the outer qml document. This means it cannot access the properties of the outer qml or QObjects registered with setContextProperty directly.


To access properties and functions of the qml document the ListItemComponent has a reference to the ListView that can be accessed by calling id.ListItem.view, with id being the id of the ListItemComponent.


 id: myComponent

myComponent.ListItem.view //the ListView
myComponent.ListItem.view.dataModel //the dataModel property of the ListView
myComponent.ListItem.view.myFunction() //a function in the ListView



Another option is to use a global context property:

// add to root context in order to be available to ListItems
QDeclarativeEngine *engine = QmlDocument::defaultDeclarativeEngine();
QDeclarativeContext *rootContext = engine->rootContext();
rootContext->setContextProperty("dateTimeUtil", DateTimeUtil::instance());

 This makes a property accessible in both normal QML and in ListItemComponents