Loop through the delegate of ListView
-
Hi,
First question :
Is there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?Second question :
Does the model item associated with the Listview are always in the same order as the delegates ?Best regards,
-
If I understand right you probably will need to loop through every model index with
QAbstractItemDelegate * QAbstractItemView::itemDelegate ( const QModelIndex & index ) const -
Hi,
First question :
Is there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?Second question :
Does the model item associated with the Listview are always in the same order as the delegates ?Best regards,
Hi @Erakis,
s there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?
You will need to iterate over the
children
ofListView
'scontentItem
eg:ListView { id: list width: 200; height: 40 model: mymodel delegate: Text { objectName: "text" text: name + ": " + number } } for(var child in list.contentItem.children) { console.log(list.contentItem.children[child].objectName) }
You can then filter using
objectName
or any other property of the delegate Item.Does the model item associated with the Listview are always in the same order as the delegates ?
AFAIK, yes. Delegates are always in the same order as of the model items.
-
Thank you for your answers.
So if the Delegates are always in the same order as of the model items I could also loop through the model items as long as they are all visible (like alex_malyy said) ? But what If I add a new children dynamically to the Listview, (without using the model) it will not be available in the model data.
I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ? Do I have to expose a property like this
Component { property variant myData: myListModel.get(index) }
to my Delegates ?
and using it like this ?
for(var child in list.contentItem.children) { console.log(list.contentItem.children[child].myData) }
It seem to work, but it is the right way ?
Best regards,
-
Thank you for your answers.
So if the Delegates are always in the same order as of the model items I could also loop through the model items as long as they are all visible (like alex_malyy said) ? But what If I add a new children dynamically to the Listview, (without using the model) it will not be available in the model data.
I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ? Do I have to expose a property like this
Component { property variant myData: myListModel.get(index) }
to my Delegates ?
and using it like this ?
for(var child in list.contentItem.children) { console.log(list.contentItem.children[child].myData) }
It seem to work, but it is the right way ?
Best regards,
But what If I add a new children dynamically to the Listview, (without using the model) it will no...
You have to add item to the model so that it reflects in
ListView
not directly.I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ?
To get the data always use the model instead of delegate. In the first question in your first post you asked for delegate so I told the way to iterate over the delegate. To get the data its good to use model.