问题
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