Search code examples
qtqmlqtquickcontrols

How to get the current row of a QML TreeView?


Is there a way to get the row number of a QML TreeView when selecting/clicking something on the TreeView? For example, for a TableView I use currentRow. Is there something equivalent for a TreeView?


Solution

  • You should use currentIndex. More info in the documentation.

    For example:

    TreeView {
        id: myTree
        ...
        model: myModel
    
        onCurrentIndexChanged: console.log("current index: " + currentIndex
                                           + " current row: " + currentIndex.row)
    
        TableViewColumn {
            title: "title1"
            role: "role1"
        }
    
        TableViewColumn {
            title: "title2"
            role: "role2"
        }
    
        onClicked: {
            console.log("clicked", index)
        }
    }
    

    You can check the complete example in GitHub.