<?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[Removed QLineEdit gets focus. Is this a bug?]]></title><description><![CDATA[<p dir="auto">I'll illustrate the problem with Qt for Python for the sake of simple setup. Exactly the same problem appears with Qt/C++.</p>
<pre><code class="language-python">import sys
from PySide2.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QFormLayout, QLineEdit, QGroupBox, QMessageBox, QWidget, QPushButton

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        #HLayout
        # Button-&gt;remove&amp;hide(Widget1)
        # GroupBox(&amp;G), VLayout
        #  Widget1, FormLayout
        #   LineEdit
        #  Widget2, FormLayout
        #   LineEdit

        layout = QHBoxLayout(self)

        group_box = QGroupBox("&amp;Group", self)
        group_layout = QVBoxLayout(group_box)

        widget1 = QWidget(self)
        form_layout1 = QFormLayout(widget1)
        line_edit = QLineEdit(widget1)
        line_edit.textEdited.connect(lambda: QMessageBox.information(self, "info", "typed"))
        form_layout1.addWidget(line_edit)
        group_layout.addWidget(widget1)

        widget2 = QWidget(self)
        form_layout1 = QFormLayout(widget2)
        line_edit = QLineEdit(widget2)
        form_layout1.addWidget(line_edit)
        group_layout.addWidget(widget2)

        layout.addWidget(group_box)

        button = QPushButton("button", self)
        button.clicked.connect(lambda: (group_layout.removeWidget(widget1), widget1.hide()))
        layout.addWidget(button)


if __name__ == "__main__":
    app = QApplication([])
    window = MyWidget()
    window.show()
    sys.exit(app.exec_())
</code></pre>
<p dir="auto">Steps to reproduce the problem: run the program, click the button, press "Alt+G", press"a". A window unexpectedly pops up.</p>
<p dir="auto">The structure of the UI is basically:</p>
<ul>
<li>QWidget with QHBoxLayout
<ul>
<li>QGroupBox("&amp;G") with QVBoxLayout
<ul>
<li>widget1 = QWidget, FormLayout<br />
- QLineEdit, textChanged.connect(popup)</li>
<li>widget2 = QWidget, FormLayout<br />
- QLineEdit</li>
</ul>
</li>
<li>QPushButton, clicked.connect(remove(widget1); widget1.hide())</li>
</ul>
</li>
</ul>
<p dir="auto">Initially, the <code>QLineEdit</code> of <code>widget1</code> gets the focus. By clicking the button, <code>widget1</code> gets hidden and removed from its parent's layout. Next, by pressing "Alt+G", the <code>QGroupBox</code> gets the focus (and is supposed to pass it on to the second <code>QLineEdit</code>). Finally, by pressing 'a', a <code>QMessageBox</code> pops up, which means that it is in fact the first <code>QLineEdit</code>, <strong>which has been removed</strong> along with its parent <code>widget1</code> from the <code>QGroupBox</code>, who gets the focus.</p>
<p dir="auto">Could anyone explain this behavior? Thanks.</p>
]]></description><link>https://forum.qt.io/topic/114741/removed-qlineedit-gets-focus-is-this-a-bug</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 20:26:13 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114741.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 May 2020 21:05:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Removed QLineEdit gets focus. Is this a bug? on Wed, 13 May 2020 09:31:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/isar">@<bdi>isar</bdi></a><br />
yes i also tried that but thought it was a bit overkill.<br />
Apparently update() is not enough always. My test was actually made in Designer and  i just use textChange signal to see where i typed and it was easy to get same effect that<br />
setting focus back to Group after i hide widget1 would still focus the hidden line edit and type to it.</p>
<p dir="auto">So for me, it smells as a bug as hidden widgets should not get keyboard focus.<br />
And the GroupBox clearly set focus to a hidden lineedit. :)</p>
]]></description><link>https://forum.qt.io/post/594426</link><guid isPermaLink="true">https://forum.qt.io/post/594426</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 13 May 2020 09:31:46 GMT</pubDate></item><item><title><![CDATA[Reply to Removed QLineEdit gets focus. Is this a bug? on Wed, 13 May 2020 09:12:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> ok it seems that <code>widget1.setParent(nullptr)</code> is the solution and it implies a removal from the layout.</p>
]]></description><link>https://forum.qt.io/post/594423</link><guid isPermaLink="true">https://forum.qt.io/post/594423</guid><dc:creator><![CDATA[isar]]></dc:creator><pubDate>Wed, 13 May 2020 09:12:03 GMT</pubDate></item><item><title><![CDATA[Reply to Removed QLineEdit gets focus. Is this a bug? on Tue, 12 May 2020 23:13:11 GMT]]></title><description><![CDATA[<p dir="auto">Many thanks for your help.<br />
Unfortunately <code>group_layout.update()</code> does not change the behavior on my side. Could you please paste a complete running example in c++/python with the fixed behavior, where "press button -&gt; Alt+G -&gt; press 'a'" gives no message box? Note that no TAB is supposed to be pressed during the process, since even without <code>group_layout.update()</code>, it's easy to see by pressing TAB a few times that the removed <code>line_edit</code> of <code>widget1</code> is already out of the tab order, but that does not justify the problem.</p>
]]></description><link>https://forum.qt.io/post/594364</link><guid isPermaLink="true">https://forum.qt.io/post/594364</guid><dc:creator><![CDATA[isar]]></dc:creator><pubDate>Tue, 12 May 2020 23:13:11 GMT</pubDate></item><item><title><![CDATA[Reply to Removed QLineEdit gets focus. Is this a bug? on Tue, 12 May 2020 21:54:33 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to the forums.</p>
<p dir="auto">Could not test your code as not python user but i could reproduce it<br />
in c++ with a similar setup.</p>
<p dir="auto">The reason is that removeWidget does NOTHING but remove it from the layout and it still has<br />
the same parent/ownership and such. So it seems that GroupBox somehow not aware it was hidden<br />
so it still has it in tabOrder list.</p>
<p dir="auto">Anyhow, in c++ it works correctly if i ask layout to update() after removing the widget.</p>
<p dir="auto">I assume something like<br />
button.clicked.connect(lambda: (group_layout.removeWidget(widget1),group_layout.update(), widget1.hide()))</p>
<p dir="auto">Then it correctly gives focus to the remaining visible linedit in widget2 and not the hidden one<br />
when i press alt+G to focus GrpBox.</p>
]]></description><link>https://forum.qt.io/post/594360</link><guid isPermaLink="true">https://forum.qt.io/post/594360</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Tue, 12 May 2020 21:54:33 GMT</pubDate></item></channel></rss>