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. Why does Tab key doesn't trigger KeyPressEvent in main widget after setting adn then clearing focus on QGraphicsTextItem in PySide2
Forum Updated to NodeBB v4.3 + New Features

Why does Tab key doesn't trigger KeyPressEvent in main widget after setting adn then clearing focus on QGraphicsTextItem in PySide2

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 208 Views 2 Watching
  • 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.
  • K Offline
    K Offline
    Ke20
    wrote on last edited by
    #1

    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_())
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      From a quick test, the tab key is used to insert tabulation in the text.

      What are you trying to achieve ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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