Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Event.modifiers() not working in combination with Qt.Key.Key_Up
Forum Updated to NodeBB v4.3 + New Features

Event.modifiers() not working in combination with Qt.Key.Key_Up

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 582 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dschiller
    wrote on last edited by SGaist
    #1

    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.

    D 1 Reply Last reply
    0
    • D dschiller

      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.

      D Offline
      D Offline
      dschiller
      wrote on last edited by
      #2

      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)
      
      1 Reply Last reply
      0
      • SGaistS SGaist has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved