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 575 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 5 May 2023, 21:06 last edited by SGaist 5 Jun 2023, 19:42
    #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 5 May 2023, 21:30
    0
    • D dschiller
      5 May 2023, 21:06

      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 5 May 2023, 21:30 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
      • S SGaist has marked this topic as solved on 6 May 2023, 19:41

      1/2

      5 May 2023, 21:06

      • Login

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