我正在使用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();
}
}
问题是treeView.currentIndex和treeView.selection.currentIndex并不相同。如果要设置 treeView.currentIndex 的值,则只能通过隐藏变量 __listView 访问它。
__listView.currentIndex = idx.row
编辑:我找到了另一个选择
treeView.__currentRow = idx.row