ListView: call method onComplete
-
Hey there,
I have a problem and it seems to be a Qt bug ^^
I have a Loader loading some qml. one of them is a MusicList.
This music list has a ListView
@ListView{
id:listView
x:140
y:37
width:474
height:100
interactive: false
model: XmlListModel{
id:musicListModelsource: "xml/musicData.xml" query: "/PLAYLIST/SONG" XmlRole{ name:"TITLE"; query:"TITLE/string()" } } spacing:33 delegate:TrackItem{ trackID: index+1 title: TITLE } Component.onCompleted: { initialize(); } }@
When the component (listView) is completed I would like to call initialize()
@function initialize()
{
console.log("init");
listView.positionViewAtIndex(3, ListView.Beginning);
}@While running I can see the output "init" but the positioning for the listview doesnt work although I followed these instructions
bq. Note: methods should only be called after the Component has completed. To position the view at startup, this method should be called by Component.onCompleted. For example, to position the view at the end: Component.onCompleted: positionViewAtIndex(count - 1, ListView.Beginning)
Later while running changing the listViews position dynamically works pretty well.
So any ideas whats happening?
-
Not Sure if this is what you mean ? but you can also control the listView via onstatuschanged in the XmlListModel
EDIT you can also add states to your listview and then uses the status of the xmllistmodel to define the states. Would be great for animations later on
@
Item {
...
XmlListModel{
id:musicListModel
source: "xml/musicData.xml"
query: "/PLAYLIST/SONG"
XmlRole{ name:"TITLE"; query:"TITLE/string()" }
onStatusChanged: {
if(status === XmlListModel.Ready){
// do your Functions here
}
if(status === XmlListModel.Error){console.log(errorString())}
if(status === XmlListModel.Loading){console.log("Loading \n" + source )}
}
}ListView{
id:listView
x:140
y:37
width:474
height:100
interactive: false
model: musicListModel
spacing:33
delegate:TrackItem{
trackID: index+1
title: TITLE
}
}
}@ -
The issue is that onCompleted is called on listView before any data is available in the model. In other words you are trying to position the view at row 3 when there are 0 rows available and the request is ignored. The workaround suggested by bob should work. You could also react to onCountChanged on the ListView as well.