ListView, PositionViewAtBeginning()
-
Hi!
I have a listview. When I click on an item, I clear the model and add new items in the model (from c++).
This works fine, and the listview is updated properly.The only problem is that the position of the listview is not changed when the model is updated. For example, if I scroll down to the end of the list, click on the last item, then, the list would be updated with new items, but I would still see the last item.
I would like to set the position to the beginning of the list when an item is clicked, but I don't know how to do that...I tried to call positionViewAtBeginning() and to set currentIndex to 0 with no luck. Any help?
Thanks!
-
Hi,
As per the docs
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, viz :
@
Component.onCompleted: positionViewAtBeginning()
@So are you calling it in the above mentioned way ?
-
Yes, I tried to add this line, but it seems that Component.onCompleted is called only once, when the component is created at launch time.
But when I remove/add data from the model, the list is updated accordingly, but onComleted is not called.
So, how to change the position of the list after the component has been created?
-
Hi,
Depending upon your use case i created a small code, it positions the item to the top after clearing the list.
May be this would be useful@
import QtQuick 2.0
Rectangle {
width: 320; height: 240ListModel { id: model ListElement { name: "PrePopulated" number: "32423434" } ListElement { name: "PrePopulated" number: "23423232" } ListElement { name: "PrePopulated" number: "23323234" } } Component { id: delegate Item { width: 320; height: 40 Column { Text { text: '<b>Name:</b> ' + name } Text { text: '<b>Number:</b> ' + number } } MouseArea { anchors.fill: parent onClicked: { //If clicked on last item in list, clear the list and populate the new items if(index===view.count-1) { console.log(index); addItem(); } } } } } ListView { id: view anchors.fill: parent model: model delegate: delegate highlight: Rectangle { color: "lightsteelblue"; radius: 5 } focus: true } //Click to add Items to listview Text { id: add text: "Add Item" anchors.bottom: parent.bottom MouseArea { anchors.fill: parent onClicked: { model.append({name: "ButtonClick", number: Math.floor((Math.random()*10000)+1).toString()} ); view.positionViewAtEnd(); } } } function addItem() { model.clear(); for(var i=0; i<10; i++) { var data = {'name': "ItemClicked " + i.toString(), 'number': Math.floor((Math.random()*10000)+1).toString() }; model.append(data); view.positionViewAtBeginning(); } }
}
@