Is it possible to deselect in a QTreeView by clicking off an item?

后端 未结 8 1213
盖世英雄少女心
盖世英雄少女心 2020-12-30 05:35

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 intercep

8条回答
  •  一生所求
    2020-12-30 06:16

    This is actually quite simple (in PyQt):

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

    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 :)

提交回复
热议问题