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. Pyside6 QWizard setFocus() not working

Pyside6 QWizard setFocus() not working

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 1.4k 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.
  • ErriezE Offline
    ErriezE Offline
    Erriez
    wrote on last edited by Erriez
    #1

    I've a PIP PySide 6.4.1 QWizard application on Ubuntu 20.04 with two textboxes. Goal is to set focus on a QLineEdit named txt_edit2 by default, but the code below ignores line txt_edit2.setFocus() and sets focus on the first txt_edit1:

    # Focus not working in QWizard!
    from PySide6.QtWidgets import (QApplication, QWizard, QWizardPage, QLineEdit, QHBoxLayout)
    import sys
    
    class ClassInfoPage(QWizardPage):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
            txt_edit2 = QLineEdit('txt_edit2: Focus not set')
    
            layout = QHBoxLayout(self)
            layout.addWidget(txt_edit1)
            layout.addWidget(txt_edit2)
    
            # setFocus() line below is ignored! txt_edit1 is still the default
            txt_edit2.setFocus()
    
    
    class MyWizard(QWizard):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.setWindowTitle("Wizard setFocus() issue")
    
            self.addPage(ClassInfoPage())
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        wizard = MyWizard()
        wizard.show()
        sys.exit(wizard.exec())
    

    Screenshot wrong focus:
    f0ea6404-cb6f-4748-b30d-d0721e44519b-image.png

    The code below works for a window:

    # Focus works in normal window!
    from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QLineEdit, QHBoxLayout
    import sys
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
    
            self.setMinimumSize(400, 100)
            self.setWindowTitle("Focus example")
    
            txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
            txt_edit2 = QLineEdit('txt_edit2: Focus not set')
    
            layout = QHBoxLayout()
            layout.addWidget(txt_edit1)
            layout.addWidget(txt_edit2)
    
            widget = QWidget()
            widget.setLayout(layout)
    
            self.setCentralWidget(widget)
    
            # setFocus() works for a normal QMainWindow application!
            txt_edit2.setFocus()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        app.exec()
    

    Screenshot correct focus:
    d213c02e-0956-4e7e-8fee-22650b5597bf-image.png

    Is setFocus() a bug in QWizard or do I something wrong?

    ErriezE 1 Reply Last reply
    0
    • ErriezE Erriez

      I've a PIP PySide 6.4.1 QWizard application on Ubuntu 20.04 with two textboxes. Goal is to set focus on a QLineEdit named txt_edit2 by default, but the code below ignores line txt_edit2.setFocus() and sets focus on the first txt_edit1:

      # Focus not working in QWizard!
      from PySide6.QtWidgets import (QApplication, QWizard, QWizardPage, QLineEdit, QHBoxLayout)
      import sys
      
      class ClassInfoPage(QWizardPage):
          def __init__(self, parent=None):
              super().__init__(parent)
      
              txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
              txt_edit2 = QLineEdit('txt_edit2: Focus not set')
      
              layout = QHBoxLayout(self)
              layout.addWidget(txt_edit1)
              layout.addWidget(txt_edit2)
      
              # setFocus() line below is ignored! txt_edit1 is still the default
              txt_edit2.setFocus()
      
      
      class MyWizard(QWizard):
          def __init__(self, parent=None):
              super().__init__(parent)
      
              self.setWindowTitle("Wizard setFocus() issue")
      
              self.addPage(ClassInfoPage())
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          wizard = MyWizard()
          wizard.show()
          sys.exit(wizard.exec())
      

      Screenshot wrong focus:
      f0ea6404-cb6f-4748-b30d-d0721e44519b-image.png

      The code below works for a window:

      # Focus works in normal window!
      from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QLineEdit, QHBoxLayout
      import sys
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super().__init__()
      
              self.setMinimumSize(400, 100)
              self.setWindowTitle("Focus example")
      
              txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
              txt_edit2 = QLineEdit('txt_edit2: Focus not set')
      
              layout = QHBoxLayout()
              layout.addWidget(txt_edit1)
              layout.addWidget(txt_edit2)
      
              widget = QWidget()
              widget.setLayout(layout)
      
              self.setCentralWidget(widget)
      
              # setFocus() works for a normal QMainWindow application!
              txt_edit2.setFocus()
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          window = MainWindow()
          window.show()
          app.exec()
      

      Screenshot correct focus:
      d213c02e-0956-4e7e-8fee-22650b5597bf-image.png

      Is setFocus() a bug in QWizard or do I something wrong?

      ErriezE Offline
      ErriezE Offline
      Erriez
      wrote on last edited by
      #2

      Workaround is calling setFocus() via a QTimer.singleShot():

      +from PySide6.QtCore import QTimer
      
      class ClassInfoPage(QWizardPage):
          def __init__(self, parent=None):
              super().__init__(parent)
      
              self.txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
              self.txt_edit2 = QLineEdit('txt_edit2: Focus not set')
      
              layout = QHBoxLayout(self)
              layout.addWidget(self.txt_edit1)
              layout.addWidget(self.txt_edit2)
      
      +        # Workaround to call setFocus() from a timer event:
      +        QTimer.singleShot(0, self.timer_event)
      
      +    def timer_event(self):
      +        self.txt_edit2.setFocus()
      

      Any suggestion why txt_edit2.setFocus() is not working from the QWizardPage constructor?

      JonBJ 1 Reply Last reply
      0
      • ErriezE Erriez

        Workaround is calling setFocus() via a QTimer.singleShot():

        +from PySide6.QtCore import QTimer
        
        class ClassInfoPage(QWizardPage):
            def __init__(self, parent=None):
                super().__init__(parent)
        
                self.txt_edit1 = QLineEdit('txt_edit1: Focused: wrong')
                self.txt_edit2 = QLineEdit('txt_edit2: Focus not set')
        
                layout = QHBoxLayout(self)
                layout.addWidget(self.txt_edit1)
                layout.addWidget(self.txt_edit2)
        
        +        # Workaround to call setFocus() from a timer event:
        +        QTimer.singleShot(0, self.timer_event)
        
        +    def timer_event(self):
        +        self.txt_edit2.setFocus()
        

        Any suggestion why txt_edit2.setFocus() is not working from the QWizardPage constructor?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Erriez said in Pyside6 QWizard setFocus() not working:

        Any suggestion why txt_edit2.setFocus() is not working from the QWizardPage constructor?

        I have a feeling this, or similar, may have been asked before. I suspect the wizard does its own focus setting as each page is shown. Therefore the technique of doing your own setFocus() on a delayed timer is required for your code to change which widget has focus.

        ErriezE 1 Reply Last reply
        0
        • JonBJ JonB

          @Erriez said in Pyside6 QWizard setFocus() not working:

          Any suggestion why txt_edit2.setFocus() is not working from the QWizardPage constructor?

          I have a feeling this, or similar, may have been asked before. I suspect the wizard does its own focus setting as each page is shown. Therefore the technique of doing your own setFocus() on a delayed timer is required for your code to change which widget has focus.

          ErriezE Offline
          ErriezE Offline
          Erriez
          wrote on last edited by
          #4

          @JonB Thanks for your confirmation. I found this suggestion somewhere on the internet (not in this Qt forum). The behavior is not clear for me when reading the documentation, but maybe I overlooked something. I'll continue with this workaround.

          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