什么可以阻止mousePressEvent或事件过滤器鼠标单击事件?

2024-10-01 17:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在QTreeWidget中我似乎无法获得任何鼠标单击。我试过。。。在

  • …覆盖mousePressEvent,但它从未运行过。甚至不记录消息。在
  • …正在使用事件文件服务器。它适用于除鼠标点击以外的所有操作。在
  • …使用委托。他们的编辑器事件可以正常工作,但只有在一个项目上,这是不够的
  • …确保所有内容都添加到布局中。输出使用的是creator和qtilayout.addWidget布局(). 我还将小部件实例添加到主窗口的布局中。在

我可以使用答案将该小部件注册为QTreeWidget的事件筛选器,如下所示:

# In __init___
    # self.tree is the QTreeWidget
    self.tree.viewport().installEventFilter(self)



def eventFilter(self, target, event):
    """ 
    This widget is an event filter for the tree, so this function is triggered 
    automatically
    """
    # Print on right-click
    if (event.type() == QEvent.MouseButtonPress and 
        event.button() == Qt.RightButton):
        print("Right Click")

    # Don't block/accept the event
    return False

Tags: 文件theself服务器eventtree消息is

热门问题