PDA

View Full Version : Access a property of delegate in listview



beemaneni
29th September 2016, 14:11
Hi,
This is pretty simple but i lost somewhere when i am working on this .Here i post my example.
I need to read a property which is declared inside a delegate from its parent.How do i do ?


ListView{
id: listView
property bool indeeee: listView.contentItem.childrenRect.check
state: listView.indeeee ? "moved" : ""
delegate: delegateItem
}
Component{
id: delegateItem
Row{
id: rowww
spacing: 20
property bool check:index===2 ? true : false
}
}

I tried to read with listView.contentItem.childrenRect.check which says unable to assign undefined to bool

Regards
Bala B

wysota
29th September 2016, 16:04
You have to remember the delegate is a component so you don't have one delegate per view but rather one delegate per visible item. To access an item from a view you have ListView.currentItem, ListView.highlightItem or ListView.itemAt.

So for example to know whether the current item is checked, you'd do:

property bool indeeee: currentItem.check

However often you want to provide a relation in the other direction:

ListView {
property int checkedIndex: -1

delegate: MouseArea {
// ...
onClicked: ListView.view.checkedIndex = index // upon clicking I become the checked index in the view
}
}

Just remember that only the delegate root (and not its children) has the "ListView" attached property and the "index" property available.