<?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[QLineEdit::editingFinished called twice if press Enter key - actual bug?!]]></title><description><![CDATA[<p dir="auto">Tested under Linux (Qt 5.9.5 released with Ubuntu 18.04), and reported to me by users under Windows (I have reason to believe they are running a later version of Qt, like 5.11+).</p>
<ol>
<li>Create a <code>QLineEdit</code>.</li>
<li>Attach an <code>editingFinished()</code> slot.</li>
<li>Make the slot put up some kind of dialog.</li>
<li>Press the <strong>Return</strong> key in the line edit to finish your editing.</li>
</ol>
<p dir="auto">You will get the dialog shown <em>twice</em>! :(</p>
<p dir="auto">Instead of <strong>Return</strong>, use, say, <strong>Tab</strong> key to finish editing.  You will only get the dialog shown once.</p>
<pre><code>import sys
from PyQt5 import QtWidgets


def lineedit_editingFinished():
    mbox = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Information, "editingFinished called", "editingFinished called")
    mbox.exec()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    dlg = QtWidgets.QDialog()
    dlg.setFixedSize(400, 300)
    dlg.setLayout(QtWidgets.QVBoxLayout())

    lineedit = QtWidgets.QLineEdit()
    lineedit.editingFinished.connect(lineedit_editingFinished)
    dlg.layout().addWidget(lineedit)

    extraLineeditJustSoYouCanTabOutOfFirstOne = QtWidgets.QLineEdit()
    dlg.layout().addWidget(extraLineeditJustSoYouCanTabOutOfFirstOne)

    dlg.show()

    sys.exit(app.exec_())
</code></pre>
<p dir="auto">I don't think it's how/what you show as a dialog.  But it needs something like that.  It's <em>something</em> to do with the line edit losing focus to the up-popping dialog.  But only when <strong>Return</strong> is pressed....</p>
<p dir="auto">If you don't open a window/dialog in the slot but just, say, send a message to console, you do <em>not</em> get the twice.  If you don't show a dialog but instead place a breakpoint, you will end up with the twice.  Again, <em>something</em> to do with the debugger swapping up-front windows, just like a dialog, causing it.</p>
<p dir="auto">Help/fix/workaround, please? :)</p>
]]></description><link>https://forum.qt.io/topic/105335/qlineedit-editingfinished-called-twice-if-press-enter-key-actual-bug</link><generator>RSS for Node</generator><lastBuildDate>Mon, 15 Jun 2026 18:08:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/105335.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Jul 2019 13:59:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Tue, 30 Jul 2019 08:54:32 GMT]]></title><description><![CDATA[<p dir="auto">The workaround is indeed:</p>
<pre><code>    if (!lineEditorWidget-&gt;isModified())
        return; // Ignore second signal.
    lineEditorWidget-&gt;setModified(false);
    // Do something with the text. 
</code></pre>
<p dir="auto">i.e. you must clear the flag <em>before</em> doing something (which will invoke a dialog or similar) to prevent second, re-entrant call of <code>editingFinished</code>.</p>
]]></description><link>https://forum.qt.io/post/543631</link><guid isPermaLink="true">https://forum.qt.io/post/543631</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 30 Jul 2019 08:54:32 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Sat, 27 Jul 2019 08:44:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>sgaist</bdi></a><br />
Thank you!  I have a couple of questions/observations:</p>
<ul>
<li>
<p dir="auto">I see this bug was actually first reported in 2005.  Is this 14-years-to-fix a record for a Qt bug? :)</p>
</li>
<li>
<p dir="auto">In the bug report link there is a <a href="https://bugreports.qt.io/browse/QTBUG-40?focusedCommentId=363578&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-363578" target="_blank" rel="noopener noreferrer nofollow ugc">workaround </a> posted by a poster.  I would like to implement this, as there is no chance of moving to a newer Qt version (I only use whatever Qt was released with the Linux version I use, so probably not until Ubuntu 20.04).  It reads as follows:</p>
</li>
</ul>
<blockquote>
<p dir="auto">I had to work around this bug once again today (Qt 5.6.1).<br />
.<br />
<code>QLineEdit</code> has a <code>isModified</code> interface. You can set this to true when <code>editingFinished</code> is emitted the first time and ignore the signal if and when it is emitted the second time.<br />
.<br />
You can also use this to ignore <code>editingFinished</code> if the text hasn't actually changed (i.e. the <code>QLineEdit</code> simply lost focus).<br />
.<br />
This is my slot/lambda:</p>
</blockquote>
<pre><code>connect(
 lineEditorWidget,
 &amp;QLineEdit::editingFinished,
 this,
[this, lineEditorWidget]()
{ if (!lineEditorWidget-&gt;isModified()) return; // Ignore second signal. lineEditorWidget-&gt;setModified(false); // Do something with the text. } 
);
</code></pre>
<p dir="auto">Reading what he has written I am trying to confirm what his code <em>really</em> means to implement.  Given the layout of his commented lines, do I presume this is intended to read as follows:</p>
<pre><code>{
    if (!lineEditorWidget-&gt;isModified())
        return; // Ignore second signal.
    // Do something with the text. 
    lineEditorWidget-&gt;setModified(false);
}
</code></pre>
<p dir="auto">Thus we only <code>setModified(false)</code> <em>after</em> we have finished our slot code?  If <code>editingFinished</code> is re-raised (by something) whilst we are <em>inside</em> the slot doing something with the text, we would execute our code again?  As I typed this, I'm thinking he does genuinely mean</p>
<pre><code>    lineEditorWidget-&gt;setModified(false);
    // Do something with the text. 
</code></pre>
<p dir="auto">which is different (you do the math): it ignores <code>editingFinished</code> being called a second time while inside the first <code>editingFinished</code>, it's a "only do this once per modification, regardless of when <code>editingFinished</code> is called"?</p>
<p dir="auto">He also says:</p>
<blockquote>
<p dir="auto">You can set this to true when <code>editingFinished</code> is emitted the first time</p>
</blockquote>
<p dir="auto">but the code does not do that, does he mean the Qt framework is doing this, not us? <a href="https://doc.qt.io/qt-5/qlineedit.html#modified-prop" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qlineedit.html#modified-prop</a> :</p>
<blockquote>
<p dir="auto">The modified flag is never read by <code>QLineEdit</code>; it has a default value of false and is changed to true whenever the user changes the line edit's contents.</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/543150</link><guid isPermaLink="true">https://forum.qt.io/post/543150</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 27 Jul 2019 08:44:37 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Fri, 26 Jul 2019 21:02:04 GMT]]></title><description><![CDATA[<p dir="auto">I've updated the link, it wasn't correctly accessible.</p>
<p dir="auto">The issue has been fixed for Qt 5.14.</p>
<p dir="auto">As for the assignment, look at the history ;-)</p>
]]></description><link>https://forum.qt.io/post/543130</link><guid isPermaLink="true">https://forum.qt.io/post/543130</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 26 Jul 2019 21:02:04 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Thu, 25 Jul 2019 19:51:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<blockquote>
<p dir="auto">It might be QTBUG-40.</p>
</blockquote>
<p dir="auto">Looks like similar wording.  And look who's been assigned to fix it! :)  But it's been there since 2010 (the 40th bug ever found), surely it would have been fixed by now? :)</p>
<p dir="auto">However, note my findings/example code --- in my case I am claiming this only happens if the <strong>Return</strong> key is pressed to cause the <code>QLineEdit::editingFinished</code>, I don't know if that's relevant or a special case?  (Oh actually I think that's what the example in the 17 Jan '19 post says.)</p>
]]></description><link>https://forum.qt.io/post/542959</link><guid isPermaLink="true">https://forum.qt.io/post/542959</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:51:13 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Thu, 25 Jul 2019 19:44:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a><br />
Thanks for replying, useful to hear.</p>
<blockquote>
<p dir="auto">A workaround (depending on your use case) is to use returnPressed() signal instead.</p>
</blockquote>
<p dir="auto">It's deliberately <code>editingFinished</code> because the user might tab out, or click elsewhere, or....  Funnily enough, in the current case the user may well have used a <code>QCompleter</code> to populate the line edit and finish editing, so again he doesn't have to press <strong>Return</strong>.  I just promised my stakeholders that these line edits would respond correctly to <em>not</em> pressing <strong>Return</strong> to finish editing!</p>
<p dir="auto">For now I've put in a "prevent-re-entrancy" flag on my <code>editingFinished</code> while the dialog is displayed to avoid bad behaviour, but I was hoping for a "nicer hack"? :)</p>
]]></description><link>https://forum.qt.io/post/542957</link><guid isPermaLink="true">https://forum.qt.io/post/542957</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:44:35 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Fri, 26 Jul 2019 20:59:16 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">It might be <a href="https://bugreports.qt.io/browse/QTBUG-40" target="_blank" rel="noopener noreferrer nofollow ugc">QTBUG-40</a>.</p>
]]></description><link>https://forum.qt.io/post/542956</link><guid isPermaLink="true">https://forum.qt.io/post/542956</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 26 Jul 2019 20:59:16 GMT</pubDate></item><item><title><![CDATA[Reply to QLineEdit::editingFinished called twice if press Enter key - actual bug?! on Thu, 25 Jul 2019 19:35:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/542898">QLineEdit::editingFinished called twice if press Enter key - actual bug?!</a>:</p>
<blockquote>
<p dir="auto">Help/fix/workaround, please? :)</p>
</blockquote>
<p dir="auto">I've encountered the same thing today :-) SOunds like a bug to me, but I have not checked the bugtracker for it yet.</p>
<p dir="auto">A workaround (depending on your use case) is to use <code>returnPressed()</code> signal instead.</p>
]]></description><link>https://forum.qt.io/post/542950</link><guid isPermaLink="true">https://forum.qt.io/post/542950</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:35:03 GMT</pubDate></item></channel></rss>