预设QML TreeView

时间:2018-03-15 11:50:24

标签: qt treeview qml qt5 qabstractitemmodel

我正在使用QML TreeView列出一些分类选项供用户选择。

使用属性TreeView.selection我已将SlectionModel分配给树视图。 我在预选项目时遇到问题。使用

treeView.selection.setCurrentIndex(idx,3)

我只设置选择模型的属性(项目被正确选择/突出显示),但treeView.currentIndex仍然无效。当使用键向上/向下时,它将跳转到第一个项目。

我错过了什么?

 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();
        }

    }

1 个答案:

答案 0 :(得分:0)

问题是treeView.currentIndex和treeView.selection.currentIndex不一样。如果你去设置treeView.currentIndex的值,只能通过隐藏变量__listView访问它。

        __listView.currentIndex = idx.row

修改 我找到了另一个选择

         treeView.__currentRow = idx.row