A colleague made a custom QMenu derivative so that one can select multiple entries before the menu closes.
It is triggered via a QToolButton.
The problem is if the menu is large enough it will overlap with the button. The item at the current cursor position then gets selected instantly when clicking the QToolButton.
How does one prevent this?
Code for my menu, I tried to ignore the first event with a Bool flag, but it doesn't work.
class StayOpenMenu(QMenu):
"""
a class that overrides the QMenu mouseReleaseEvent to let the menu stay open when an element is selected
"""
def __init__(self, parent=None):
self.isfirstEvent = True
super().__init__("Stay open Menu", parent=parent)
def mouseReleaseEvent(self, a0: QMouseEvent):
if self.isfirstEvent:
a0.ignore()
self.isfirstEvent = False
return
try:
action = self.actionAt(a0.pos())
action.trigger()
except:
pass
def aboutToShow(self):
self.isfirstEvent = True
return super().aboutToShow()
def aboutToHide(self):
self.isfirstEvent = True
return super().aboutToShow()
Image: Before clicking the button
Image: After clicking the QToolButton