I’ve added some keyboard shortcuts to my app (PyQt5), like so:
w = MyWidget()
QShortcut(QKeySequence("Ctrl+Up"), w).activated.connect(some_function)
Unfortunately, when w
has a child widget of type QComboBox
, and the popup window of the combobox is active, the keyboard shortcuts don’t work. I’ve tried subclassing QComboBox
so that it adds an event filter to the popup view:
class Combo(QComboBox):
def __init__(self, parent):
super().__init__(parent)
def showPopup(self):
self.view().installEventFilter(self)
super().showPopup()
def eventFilter(self, source, e):
if (e.type() == QEvent.KeyPress
and (e.modifiers() & QtCore.Qt.ControlModifier
or e.modifiers() & QtCore.Qt.AltModifier)):
self.keyPressEvent(e)
return True
return super().eventFilter(source, e)
def keyPressEvent(self, e):
super().keyPressEvent(e)
self.parent().keyPressEvent(e)
I manually call self.parent().keyPressEvent()
because the events don’t seem to propagate to MyWidget
otherwise. However, even when the events do propagate to MyWidget.keyPressEvent()
thanks to my manual propagation, the keyboard shortcuts still aren’t activated.
How can I make sure that the keyboard shortcuts “see” these events? Do I need to emit a signal somewhere?