Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2817

Problem with the Tab Key after setting focus on a QGraphicsTextItem

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 5.15.3
    • PySide
    • None
    • Windows

    Description

      In the following code snippet, there is a strange behaviour I can't manage to understand. After I double click on the QGraphicsTextItem, the Tab key (and not any other key as far as I'm aware) is not triggering anymore the KeyPressEvent. It seems to come from the way PySide2 handle the focus in this case, and my blind guess is that it's linked to the fact that one of the QtCore.Qt.FocusReason is linked to the tab key (TabFocusReason).

      import sys
      from PySide2 import QtWidgets, QtCore, QtGui
      
      class MainWindow(QtWidgets.QMainWindow):
          def __init__(self):
              super().__init__()
              # Create a QGraphicsScene
              self.scene = QtWidgets.QGraphicsScene()
              
              # Create QGraphicsTextItem
              text_item = QtWidgets.QGraphicsTextItem("Example")
              text_item.setDefaultTextColor(QtCore.Qt.blue)  # Optionally set text color
              self.scene.addItem(text_item)
               
              # Create QGraphicsView
              self.view = QtWidgets.QGraphicsView(self.scene)
              self.setCentralWidget(self.view)
      
              # Store the text item for later use
              self.text_item = text_item
      
          def keyPressEvent(self, event):
              """For debugging purposes. If a key triggers the event, it prints its enum value."""
              print(event.key())
              super().keyPressEvent(event)
      
          def mouseDoubleClickEvent(self, event):
              # get the item we are clicking on
              scene_pos = self.view.mapToScene(event.pos())
              item = self.scene.itemAt(scene_pos, QtGui.QTransform())
      
              if isinstance(item, QtWidgets.QGraphicsTextItem):
                  # minimalistically reproduces the bug
                  item.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction)
                  item.setFocus(QtCore.Qt.MouseFocusReason)
              
                  item.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
                  item.clearFocus()
              
                  # Set the plain text to show the double click event worked
                  item.setPlainText("changed")
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          window = MainWindow()
          window.show()
          sys.exit(app.exec_()) 

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            kevinbp Kevin BORJON-PIRON
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes