Access to DelegateModel items properties
-
Hi all
I have GridView control managed by a DelegateModel. I need to access to the GridView items properties but I can not find a way to get the item object. Something like:
GridView { cellWidth: 80 cellHeight: 80 model: DelegateModel { id: myDelegateModel delegate: MyItemDelegate {} model: myModel } }
The MyItemDelegate is basically:
Rectangle { width: 50 height: 50 property int myProperty: 0 }
Supposing I have in the model a list of 10 items how is possible to read the MyProperty value of the items 4 showed into the GridView for example?
-
-
Hi
Thank you for your reply. I already followed the way to use contentItem.children but it seem not all the children object are my delegates. I created a gridview and I expected to see ordered into contentItem.children all the delegated showed into the view. However, I don't know why, the object at position 1 was a generic QtQuick object instead of one of my delegates at view position 1. However it seem the most correct way to proceed than I'll try to investigate more... -
@Suppaman
Qt's views indeed do not provide an easy way to access its delegates by (model) index, mainly because , by default, not all delegates are instantiated at all times. This is unlike theRepeater
, which instantiates all delegates at creation time and which provides theitemAt(index)
method.The best thing is probably not to have any properties defined in your delegate but instead define them in your model. Alas, this is not always possible, e.g. if your model is read-only.
Apart from that, you can set the
cacheBuffer
property of your view to an appropriate value to ensure that all delegates will be instantiated at all times; for large models however this may not be economical.Even so, the question then still remains of how to retrieve only the delegates from
contentItem.children
based on their model index and ignore other, non-delegate children. Instead of looping through all children to check their object type, you could add the instantiated delegates to a list (e.g. in theComponent.onCompleted
handler of the delegate).Good luck.