以编程方式获取和设置 QTreeview 中的活动行 (PyQt)
有没有办法获取和更改 QTreeView (不是 QTreeWidget)中的活动行?我所说的活动是指具有焦点突出显示的行,而不是选定的行。在绘制事件中,我可以使用 QStyle.State_HasFocus 来获取活动行,但这似乎在其他地方不起作用。
Is there a way to get and change the active row in a QTreeView (not QTreeWidget)? By active, I mean the row with the focus highlight, not the selected row. In the paint event, I can use QStyle.State_HasFocus
to get the active row, but this doesn't seem to work elsewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
currentIndex()
和setCurrentIndex()
函数获取/设置活动行,您可以在QTreeView
和中找到这些函数QItemSelectionModel
(后者由QTreeView.selectionModel()
返回)。尽管名称如此,
QItemSelectionModel
仍独立处理视图当前项和视图选择。You can get/set the active row with the
currentIndex()
andsetCurrentIndex()
functions that you can find in bothQTreeView
andQItemSelectionModel
(the latter is returned byQTreeView.selectionModel()
).And despite its name, the
QItemSelectionModel
handles the view current item, and the view selection independently.当前项目是由焦点矩形指示的项目。您可以使用树视图的 selectionModel 功能来更改它。如果您不想更改当前选定的项目,请将 QtGui.QItemSelectionModel.NoUpdate 作为第二个参数传递给 setCurrentIndex 方法。下面是一个示例:
这应该将当前项目移动到索引为 3 的项目
希望这有帮助,问候
Current item is the one which is indicated by the focus rectangle. You can change it using selectionModel function of the tree view. If you don't want to change currently selected items, pass QtGui.QItemSelectionModel.NoUpdate as a second parameter to setCurrentIndex method. Below is an example:
this should move current item to the item with index 3
hope this helps, regards
对我来说,问这样的问题并没有什么新意,因为很简单;您可以使用 Qt-Designer 并创建
QTreeView
,然后创建行编辑,然后使用操作编辑器链接它们,然后将 UI 文件转换为 Py 文件,然后您将看到幕后的工作方式。如果您尝试以下操作,您会发现这一点:
For me it got nothing here new to ask such question, why because simple; you can use Qt-Designer and create
QTreeView
and then create line edit, then link them using the action editor, then transform the UI file to Py file, then you will see how things work behind the scene.You will find this if you try: