问题描述
我希望能够通过单击 QTreeView 中没有项目的一部分来取消选择 QTreeView 中的项目,但我似乎无法找到这样做的方法.我会拦截不在项目上的点击,但 QTreeView 没有 clicked
信号,所以我不知道如何做到这一点.
I'd like to be able to deselect items in my QTreeView by clicking in a part of the QTreeView with no items in, but I can't seem to find anyway of doing this. I'd intercept a click that's not on an item, but the QTreeView doesn't have a clicked
signal, so I can't work out how to do this.
推荐答案
这其实很简单(在 PyQt 中):
This is actually quite simple (in PyQt):
class DeselectableTreeView(QtGui.QTreeView):
def mousePressEvent(self, event):
self.clearSelection()
QtGui.QTreeView.mousePressEvent(self, event)
Qt 使用 mousePressEvent
来发出 clicked
.如果您在发送事件之前清除选择,那么如果单击某个项目,它将被选中,否则将不会选择任何内容.非常感谢帕特里斯帮助我解决这个问题:)
Qt uses mousePressEvent
to emit clicked
. If you clear the selection before sending on the event, then if an item is clicked it will be selected, otherwise nothing will be selected. Many thanks to Patrice for helping me out with this one :)
这篇关于是否可以通过单击项目在 QTreeView 中取消选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!