Search code examples
qttreeviewqmlqtquick2qtquickcontrols

Deselect an index of TreeView from QML


I would like to deselect an index in a Treeview programmatically and from QML. I have a function that accepts the QModelIndex of the tree node that needs to be deselected and I do the following:

function deselectNode(index)
{
 myTreeView.selection.select(index, ItemSelectionModel.Deselect)
} 

I expect this function to trigger the selectionChanged event on the tree and deselect that index, but it does nothing. I also tried with Toggle. Is there another way to deselect just one single node from tree?

In other words, I want to do what Cmd/Ctrl + Click on a tree node does, but in Qml.


Solution

  • Actually the problem was that I was setting the currentIndex of my tree also in another place. So the function I mentioned in my question actually works fine and can deselect a node from the tree programmatically.