相关文章推荐
打篮球的充值卡  ·  Python ...·  5 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using QML TreeView to list some categorized options for user to select.

Using property TreeView.selection I have assigned SlectionModel to the treeview. I have a problem with preselecting the item. Using

treeView.selection.setCurrentIndex(idx,3)

I only set the properties of the selection model(the item gets correctly selected/higlighted), but treeView.currentIndex is still invalid. And when using key up/down, it will jump to the first item.

What am I missing?

 ItemSelectionModel {
        id: treeViewSelectionModel
        objectName: "treeViewSelectionModel"
        model: myModel
        onCurrentChanged:{console.log("Selectio - current changed from ",previous, " to ", current)}
    TreeView {
        focus: true
        id: treeView
        objectName: "treeView"
        headerVisible: false    //to hide the header
        TableViewColumn {
            title: "Name"
            role: "name"
        model: myModel
        selectionMode: SelectionMode.SingleSelection
        selection: treeViewSelectionModel
        Component.onCompleted: {
            var idx = treeView.model.getPreselected();
            console.log("preselected",idx);
          treeView.selection.setCurrentIndex(idx,ItemSelectionModel.Select);
            treeView.selection = treeView.selection
            //These logged current indexes does not match
            console.log("treeView.currentIndex",treeView.currentIndex);
            console.log("treeView.selection.currentIndex",treeView.selection.currentIndex);
            updateGuiSize();
            treeView.forceActiveFocus();

The problem is that the treeView.currentIndex and treeView.selection.currentIndex are not the same. If you go to set the value of the treeView.currentIndex, it is accessible only through the hidden variable __listView.

        __listView.currentIndex = idx.row

EDIT: I found another option

         treeView.__currentRow = idx.row
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.