<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Pyside6 QWizard setFocus() not working]]></title><description><![CDATA[<p dir="auto">I've a PIP PySide 6.4.1 <code>QWizard</code> application on Ubuntu 20.04 with two textboxes. Goal is to set focus on a <code>QLineEdit</code> named <code>txt_edit2</code> by default, but the code below ignores line <code>txt_edit2.setFocus()</code> and sets focus on the first <code>txt_edit1</code>:</p>
<pre><code># 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())
</code></pre>
<p dir="auto"><strong>Screenshot wrong focus:</strong><br />
<img src="https://ddgobkiprc33d.cloudfront.net/4da68d4d-253e-40e3-bbdc-cb97413ab91a.png" alt="f0ea6404-cb6f-4748-b30d-d0721e44519b-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto"><strong>The code below works for a window:</strong></p>
<pre><code class="language-python"># 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()
</code></pre>
<p dir="auto"><strong>Screenshot correct focus:</strong><br />
<img src="https://ddgobkiprc33d.cloudfront.net/a86df20f-309a-4da6-9319-067668b435a7.png" alt="d213c02e-0956-4e7e-8fee-22650b5597bf-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Is <code>setFocus()</code> a bug in <code>QWizard</code> or do I something wrong?</p>
]]></description><link>https://forum.qt.io/topic/142039/pyside6-qwizard-setfocus-not-working</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 06:59:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/142039.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Jan 2023 13:13:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Pyside6 QWizard setFocus() not working on Mon, 09 Jan 2023 18:40:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> 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.</p>
]]></description><link>https://forum.qt.io/post/743148</link><guid isPermaLink="true">https://forum.qt.io/post/743148</guid><dc:creator><![CDATA[Erriez]]></dc:creator><pubDate>Mon, 09 Jan 2023 18:40:25 GMT</pubDate></item><item><title><![CDATA[Reply to Pyside6 QWizard setFocus() not working on Mon, 09 Jan 2023 14:40:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/erriez">@<bdi>Erriez</bdi></a> said in <a href="/post/742987">Pyside6 QWizard setFocus() not working</a>:</p>
<blockquote>
<p dir="auto">Any suggestion why txt_edit2.setFocus() is not working from the QWizardPage constructor?</p>
</blockquote>
<p dir="auto">I have a <em>feeling</em> this, or similar, may have been asked before.  I <em>suspect</em> the wizard does its own focus setting as each page is shown.  Therefore the technique of doing your own <code>setFocus()</code> on a delayed timer is required for your code to change which widget has focus.</p>
]]></description><link>https://forum.qt.io/post/743102</link><guid isPermaLink="true">https://forum.qt.io/post/743102</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 09 Jan 2023 14:40:23 GMT</pubDate></item><item><title><![CDATA[Reply to Pyside6 QWizard setFocus() not working on Sun, 08 Jan 2023 14:45:12 GMT]]></title><description><![CDATA[<p dir="auto">Workaround is calling <code>setFocus()</code> via a <code>QTimer.singleShot()</code>:</p>
<pre><code class="language-diff">+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()
</code></pre>
<p dir="auto">Any suggestion why <code>txt_edit2.setFocus()</code> is not working from the <code>QWizardPage</code> constructor?</p>
]]></description><link>https://forum.qt.io/post/742987</link><guid isPermaLink="true">https://forum.qt.io/post/742987</guid><dc:creator><![CDATA[Erriez]]></dc:creator><pubDate>Sun, 08 Jan 2023 14:45:12 GMT</pubDate></item></channel></rss>