Unsolved GridVIew last item auto selected on insert
-
Hi,
I am struggling with simple issue . Every time I insert an item in my GridView, the last inserted item is selected. That is, last inserted item automatically becomes currentItem/currentIndex.How can I avoid that?
-
Hi @nminkov , i guess you must be handling some focus stuffs in the gridview, by default there is no auto select or auto highlight feature in GridView, it would be better if you would post your sample code.
Here is a sample code please have a look into it:-
ListModel {
id: dummyModelListElement { name: 0 } ListElement { name: 1 } ListElement { name: 2 } ListElement { name: 3 } } GridView { id: gridView width: parent.width; height: 200 model: dummyModel delegate: Rectangle { width: 100 height: 100 color: "cyan" border.color: GridView.isCurrentItem ? "red" : "black" border.width: 4 Text { anchors.centerIn: parent text: name } MouseArea { anchors.fill: parent onClicked: { gridView.currentIndex = index } } } } Button { text: "Add Item" anchors.centerIn: parent onClicked: { dummyModel.append({"name": dummyModel.count}) } }
Sample Output:-
Note:- Highlighted Rectangle will have a red border, on clicking the Rectangle iam changing the current index of the gridview and on clicking on add item iam inserting a item into the model.
-
@Shrinidhi-Upadhyaya Thanks for the complete answer. It helped me track the issue to the insert function.
In my model, I was using beginInsertRows(QModelIndex(), 0, 0), which inserted rows in front instead of back. It was unintended and caused to display the wrong text under every element. That made me believe the actual selected element was wrong.