Warm tip: This article is reproduced from serverfault.com, please click

qt-是否可以通过单击某个项目在QTreeView中取消选择?

(qt - Is it possible to deselect in a QTreeView by clicking off an item?)

发布于 2010-05-03 21:18:16

我希望能够通过单击QTreeView中没有项目的部分来取消选择QTreeView中的项目,但是我似乎还是找不到这样做的方法。我截获了不在项目上的点击,但是QTreeView没有clicked信号,所以我不知道该怎么做。

Questioner
Skilldrick
Viewed
0
Skilldrick 2010-05-05 04:59:12

这实际上非常简单(在PyQt中):

class DeselectableTreeView(QtGui.QTreeView):
    def mousePressEvent(self, event):
        self.clearSelection()
        QtGui.QTreeView.mousePressEvent(self, event)

Qt用于mousePressEvent发射clicked如果在发送事件之前清除了选择,则如果单击了某个项目,则将其选中,否则将不会选择任何内容。非常感谢Patrice帮助我解决了这一问题:)