如何为QTreeView项设置键盘焦点?

时间:2016-07-19 10:27:28

标签: qt python-3.x focus pyqt5 qtreeview

我正在使用python3.5和PyQt 5.6和Qt 5.6.1

在我的应用https://github.com/barry-scott/scm-workbench中,我有一个想要选择项目的QTreeView。我可以使用鼠标选择项目,键盘光标键可以相对于所选项目移动。

但是,如果我使用以下内容在app启动时选择一个特定的项目:

tree_view = QtWidget.QTreeView()
selection_model = tree_view.selectionModel()
selection_model.select( index,
            selection_model.Clear |
            selection_model.Select |
            selection_model.Current )

tree_view.scrollTo( index )

索引表示的项目按预期在QTreeView中高亮显示。

但是当我按下向下光标键时,选择了第一个项目,而不是已经选择的项目之后的项目。

如何让键盘导航相对于所选项目?

1 个答案:

答案 0 :(得分:1)

QTreeView的setCurrentIndex(modelIndex)功能应该做你想要的。

http://doc.qt.io/qt-5/qabstractitemview.html#setCurrentIndex