What can block mousePressEvent or Event Filter Mouse Click Events?

落爺英雄遲暮 提交于 2019-12-11 16:22:33

问题


I can't seem to get any mouse clicks in a QTreeWidget. I have tried...

  • ...overriding mousePressEvent, but it never runs at all. Not even to log a message.
  • ...using an event filer. It works for everything but mouse clicks.
  • ...using delegates. Their editor events work fine, but only when over an item, which isn't enough
  • ...making sure everything is being added to layouts. I used QTCreator and the output is using layout.addWidget(). I am also adding the widget instance to a layout in the main window.

I was able to use the answer to register the widget as an event filter for the QTreeWidget like so:

# 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

回答1:


because what you can see (and click) on QTreeWidget is actually it's viewport(). You sholud install event filter on it's viewport() instead.



来源:https://stackoverflow.com/questions/11198158/what-can-block-mousepressevent-or-event-filter-mouse-click-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!