Qt忽略QItemSelectionModel中的右键单击

Qt忽略QItemSelectionModel中的右键单击,qt,mouseevent,qtreeview,qtreewidget,selectionmodel,Qt,Mouseevent,Qtreeview,Qtreewidget,Selectionmodel,是否仍然可以检测QItemSelectionModel中单击的鼠标按钮 我想阻止鼠标右键单击以更改选择 我使用的是QTreeWidget,所以如果有一种方法可以屏蔽整个内容,那就太好了,但是右键单击仍然用于上下文菜单,所以我没有继续这种思路 仍然在尝试。。。我偶然发现了这一点,但我无法运行该函数: 这意味着一个简单的覆盖,但这在Python中不起作用 def mousePressEvent(self, mouse_event): super(MyTreeWidget, self).mou

是否仍然可以检测QItemSelectionModel中单击的鼠标按钮

我想阻止鼠标右键单击以更改选择

我使用的是QTreeWidget,所以如果有一种方法可以屏蔽整个内容,那就太好了,但是右键单击仍然用于上下文菜单,所以我没有继续这种思路

仍然在尝试。。。我偶然发现了这一点,但我无法运行该函数: 这意味着一个简单的覆盖,但这在Python中不起作用

def mousePressEvent(self, mouse_event):
    super(MyTreeWidget, self).mousePressEvent(mouse_event)
    print "here %s" % event.type()

这感觉像是另一种解决方法,但我已经成功了。在本例中,SelectionModel也是一个事件过滤器,用于从QTreeWidget的viewport()获取鼠标单击事件

另见:

(希望我没有遗漏任何东西,因为我在运行中对其进行了黑客攻击,我的实际实现稍微复杂一些,并且使用了一个单独的事件过滤器。)

from PyQt4.QtGui import QItemSelectionModel
from PyQt4.QtCore import QEvent
from PyQt4.QtCore import Qt

# In the widget class ('tree' is the QTreeWidget)...
    # In __init___ ...
        self.selection_model = CustomSelectionModel(self.tree.model())
        self.tree.viewport().installEventFilter(self.selection_model)

# In the selection model...
class CustomSelectionModel(QItemSelectionModel):
    def __init__(self, model):
        super(CustomSelectionModel, self).__init__(model)
        self.is_rmb_pressed = False

    def eventFilter(self, event):
        if event.type() == QEvent.MouseButtonRelease:
            self.is_rmb_pressed = False
        elif event.type() == QEvent.MouseButtonPress:
            if event.button() == Qt.RightButton:
                self.is_rmb_pressed = True
            else:
                self.is_rmb_pressed = False

    def select(self, selection, selectionFlags):
        # Do nothing if the right mouse button is pressed
        if self.is_rmb_pressed:
            return

        # Fall through. Select as normal
        super(CustomSelectionModel, self).select(selection, selectionFlags)