Event.modifiers() not working in combination with Qt.Key.Key_Up
-
I have an issue with combining cmd key press and arrow key up. It seems the moment i press the arrow key up the modifier (cmd) gets falsy.
def keyPressEvent(self, event: QKeyEvent) -> None: if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_W: self.scrollToTop() self.setCurrentRow(0) return super(type(self), self).keyPressEvent(event)
Works when pressing cmd + w in a QWidgetList to go to the first list item but the following example doesn't work and the first item doesn't get selected. Instead the next list item will be selected and the if branch doesnt pass with true:
def keyPressEvent(self, event: QKeyEvent) -> None: if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_Up: self.scrollToTop() self.setCurrentRow(0) return super(type(self), self).keyPressEvent(event)
Any ideas appreciated.
-
I have an issue with combining cmd key press and arrow key up. It seems the moment i press the arrow key up the modifier (cmd) gets falsy.
def keyPressEvent(self, event: QKeyEvent) -> None: if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_W: self.scrollToTop() self.setCurrentRow(0) return super(type(self), self).keyPressEvent(event)
Works when pressing cmd + w in a QWidgetList to go to the first list item but the following example doesn't work and the first item doesn't get selected. Instead the next list item will be selected and the if branch doesnt pass with true:
def keyPressEvent(self, event: QKeyEvent) -> None: if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_Up: self.scrollToTop() self.setCurrentRow(0) return super(type(self), self).keyPressEvent(event)
Any ideas appreciated.
The solution is to use bitwise AND (
&
) instead of equality operator (==
):def keyPressEvent(self, event: QKeyEvent) -> None: if event.modifiers() & Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_Up: self.setCurrentRow(0) if event.modifiers() & Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_Down: self.setCurrentRow(self.count()-1) return super(type(self), self).keyPressEvent(event)
-
S SGaist has marked this topic as solved on