make buttons process events
-
Been busy for a while so I haven't been on the site in forever, but I knew I'd receive some quality input for an issue I'm having so here goes.
I have a scrollable panel of QToolButton subclassed widgets that each contain an image and my complication is that I want to do more with the buttons than a simple left-click. I want to accept modifiers with the click, like meta key or right-click instead. However, the QToolButton only processes the "clicked" slot, and without any params.
I tried to wedge the desired behaviour in by reimplementing the keyReleaseEvent or mouseReleaseEvents but no joy. Id expect that if I reimplement either of those then I'd have to also call the correct parent version of the function as well, but when I do the interface locks up. I'm doing in pyqt5 as
def mouseReleaseEvent(self, _e): # do some stuff if event involves right click self.parentWidget().mouseReleaseEvent(_e)
What would be the correct way to handle a right click in a QToolButton subclassed widget?
using qt5 under X11 in Debian 11 -
@Kent-Dorfman said in make buttons process events:
Thanks for the followup...
Two collateral questions are important:
- must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.
What is getParentWidget()? If that's something like QWidget::parentWidget(), that would be a very unusual thing to do.
Hopefully you mean super().mouseReleaseEvent(event), with super called as a function rather than used as an object.
Call the base class version of the event handler if you want the functionality implemented by the base class. Don't call it if you want to prevent that functionality. Sometimes the documentation explains when this is necessary for particular widgets.
- what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?
There's no need to override keyboard events to get modifier keys. Either use the static QGuiApplication::keyboardModifiers() mentioned by @Pl45m4, or QInputEvent::modifiers() from the event passed into your mouse event handler.
-
I want to accept modifiers with the click, like meta key or right-click instead
Just off the top of my head, I would do it like you did and capture the key modifiers and/or pressed keys:
In
mouseReleaseEvent
Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers(); // if key x, y, z pressed while click happens, // do something
(I know it's C++ code but should also work in PyQt)
-
Thanks for the followup...
Two collateral questions are important:
-
must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.
-
what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?
yes, pyqt is pretty much a one for one in functionality to the C++ API.
-
-
@Kent-Dorfman said in make buttons process events:
Thanks for the followup...
Two collateral questions are important:
- must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.
What is getParentWidget()? If that's something like QWidget::parentWidget(), that would be a very unusual thing to do.
Hopefully you mean super().mouseReleaseEvent(event), with super called as a function rather than used as an object.
Call the base class version of the event handler if you want the functionality implemented by the base class. Don't call it if you want to prevent that functionality. Sometimes the documentation explains when this is necessary for particular widgets.
- what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?
There's no need to override keyboard events to get modifier keys. Either use the static QGuiApplication::keyboardModifiers() mentioned by @Pl45m4, or QInputEvent::modifiers() from the event passed into your mouse event handler.
-
Thanks for super()...It's been a while since I've had to program and had not considered that the super needed to be called as a function. That solved my hanging problem.
keyboard events are not an issue now since I can properly trap the right-click now with mouseReleaseEvent()
-