Qt忽略在QItemSelectionModel中右键单击

时间:2012-06-22 23:17:07

标签: qt mouseevent qtreeview qtreewidget selectionmodel

无论如何都要检测QItemSelectionModel内部点击了哪个鼠标按钮?

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

我使用它是一个QTreeWidget,所以如果有办法掩盖整个事情,那就太好了,但右键单击仍然用于上下文菜单,所以我没有追求这个思路

还在尝试......我偶然发现了这个但是我无法运行这个功能:http://qt-project.org/faq/answer/how_to_prevent_right_mouse_click_selection_for_a_qtreewidget 这意味着一个简单的覆盖,但这在Python中不起作用

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

1 个答案:

答案 0 :(得分:1)

这感觉就像是另一种解决方法,但我得到了它的工作。在此示例中,SelectionModel也是一个事件过滤器,它从QTreeWidget的视口中获取鼠标单击事件()

另见:

(希望我没有留下任何东西,因为我在运行中黑下攻击,我真正的实现有点复杂,并使用单独的事件过滤器。)

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)
相关问题