Pyside6 QWizard setFocus() not working
-
I've a PIP PySide 6.4.1
QWizardapplication on Ubuntu 20.04 with two textboxes. Goal is to set focus on aQLineEditnamedtxt_edit2by default, but the code below ignores linetxt_edit2.setFocus()and sets focus on the firsttxt_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:

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:

Is
setFocus()a bug inQWizardor do I something wrong? -
I've a PIP PySide 6.4.1
QWizardapplication on Ubuntu 20.04 with two textboxes. Goal is to set focus on aQLineEditnamedtxt_edit2by default, but the code below ignores linetxt_edit2.setFocus()and sets focus on the firsttxt_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:

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:

Is
setFocus()a bug inQWizardor do I something wrong?Workaround is calling
setFocus()via aQTimer.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 theQWizardPageconstructor? -
Workaround is calling
setFocus()via aQTimer.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 theQWizardPageconstructor?@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. -
@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.