Deselect an index from TreeView in QML



  • Hi,
    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.


  • Moderators

    @ehsanen Did you set ItemSelectionModel for TreeView ?

    selection: ItemSelectionModel {
        model: myModel
    }
    

    However it didnt work with the SingleSelection (possibly a bug?) but it does if you set other mode for eg. ExtendedSelection

    selectionMode: SelectionMode.ExtendedSelection
    


  • Sorry my mistake. The code I provided actually works, the problem was I was selecting another node of the tree mistakenly in another part of my code.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.