2012-06-22 31 views
0

有没有办法检测到从QItemSelectionModel中点击了哪个鼠标按钮?Qt忽略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
工作

这感觉还像另一个解决方法,但我得到它的工作。在这个例子中,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)