Solved Listview currentIndex increments after inserting item in model
-
Hi,
Did anyone noticed this issue?
Btw, what i am trying to do is update the element, i used remove() and then insert() to update the currentItem, however after the insertion the currentIndex incremented. I wanted to remain the currentIndex to the previously selected item.
Thanks.
-
Hi,
I don't think this is an issue but a desired behavior when you insert an item in the model before the current itemcurrentIndex to avoid changing the currentItem.
Why don't you use set method to update the currentItem instead of removing/inserting ?Here is a small example showing insert/remove/set/append :
import QtQuick 2.0 import QtQuick.Window 2.0 import QtQuick.Controls 1.4 Window{ visible:true; width:640; height:480; Component{ id:controlPanel Flow{ width: parent.width height:50 TextField{ id:name } Button{ text:"Add head" onClicked: { lstModel.insert(0,{"text":name.text}) } } Button{ text:"Add tail" onClicked: { lstModel.append({"text":name.text}) } } Button{ text:"Update current" onClicked: { lstModel.set(lstView.currentIndex, {"text":name.text}) } } Button{ text:"Current index:"+lstView.currentIndex+ (lstView.highlightFollowsCurrentItem ? "(follow)":"") onClicked: { lstView.highlightFollowsCurrentItem =!lstView.highlightFollowsCurrentItem } } Button{ text:"previous" onClicked: lstView.decrementCurrentIndex() } Button{ text:"next" onClicked: lstView.incrementCurrentIndex() } Button{ text:"Remove selected" onClicked: { if (lstView.currentIndex!= -1){ lstModel.remove(lstView.currentIndex,1); lstView.currentIndex = -1; } } } Button{ text:"unselected" onClicked: lstView.currentIndex = -1; } } } ListModel{ id:lstModel ListElement{ text:"one item" } ListElement{ text:"another one" } } ListView{ id:lstView anchors.fill: parent header: controlPanel model:lstModel delegate:Text{ text: model.text MouseArea{ anchors.fill: parent onClicked: lstView.currentIndex = index } } highlight:Rectangle{ color:lstView.currentItem ? "blue" : "transparent" } } }
-
@Charby Thanks!
Wow how can I miss the set() property.