以编程方式获取和设置 QTreeview 中的活动行 (PyQt)

发布于 2024-12-05 08:06:30 字数 133 浏览 6 评论 0原文

有没有办法获取和更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

峩卟喜欢 2024-12-12 08:06:30

您可以使用 currentIndex()setCurrentIndex() 函数获取/设置活动行,您可以在 QTreeView中找到这些函数QItemSelectionModel(后者由QTreeView.selectionModel()返回)。

尽管名称如此,QItemSelectionModel 仍独立处理视图当前项和视图选择。

You can get/set the active row with the currentIndex() and setCurrentIndex() functions that you can find in both QTreeView and QItemSelectionModel (the latter is returned by QTreeView.selectionModel()).

And despite its name, the QItemSelectionModel handles the view current item, and the view selection independently.

乖不如嘢 2024-12-12 08:06:30

当前项目是由焦点矩形指示的项目。您可以使用树视图的 selectionModel 功能来更改它。如果您不想更改当前选定的项目,请将 QtGui.QItemSelectionModel.NoUpdate 作为第二个参数传递给 setCurrentIndex 方法。下面是一个示例:

index = model.index(3, 0);
view.selectionModel().setCurrentIndex(index, QtGui.QItemSelectionModel.NoUpdate)

这应该将当前项目移动到索引为 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:

index = model.index(3, 0);
view.selectionModel().setCurrentIndex(index, QtGui.QItemSelectionModel.NoUpdate)

this should move current item to the item with index 3

hope this helps, regards

对我来说,问这样的问题并没有什么新意,因为很简单;您可以使用 Qt-Designer 并创建 QTreeView ,然后创建行编辑,然后使用操作编辑器链接它们,然后将 UI 文件转换为 Py 文件,然后您将看到幕后的工作方式。
如果您尝试以下操作,您会发现这一点:

QtCore.QObject.connect(self.treeView, QtCore.SIGNAL(_fromUtf8("clicked(QModelIndex)")), self.test)

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:

QtCore.QObject.connect(self.treeView, QtCore.SIGNAL(_fromUtf8("clicked(QModelIndex)")), self.test)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文