首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在QML currentIndex TreeView中预置TreeView

在QML currentIndex TreeView中预置TreeView
EN

Stack Overflow用户
提问于 2018-03-15 11:50:23
回答 1查看 1.2K关注 0票数 1

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

使用属性TreeView.selection,我已将SlectionModel分配给treeview。我在预选这个项目上有问题。使用

treeView.selection.setCurrentIndex(idx,3)

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

我遗漏了什么?

代码语言:javascript
复制
 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();
        }

    }
EN

回答 1

Stack Overflow用户

发布于 2018-03-29 14:31:54

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

代码语言:javascript
复制
        __listView.currentIndex = idx.row

编辑:我找到了另一个选项

代码语言:javascript
复制
         treeView.__currentRow = idx.row
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49298728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档