我使用QML TreeView列出一些分类选项,供用户选择。
使用属性TreeView.selection,我已将SlectionModel分配给treeview。我在预选这个项目上有问题。使用
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();
}
}发布于 2018-03-29 14:31:54
问题是treeView.currentIndex和treeView.selection.currentIndex不一样。如果要设置treeView.currentIndex的值,则只能通过隐藏变量__listView访问它。
__listView.currentIndex = idx.row编辑:我找到了另一个选项
treeView.__currentRow = idx.rowhttps://stackoverflow.com/questions/49298728
复制相似问题