<?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[QCompleter for QLineEdit documentation and behaviour question]]></title><description><![CDATA[<p dir="auto">I have two questions about attaching a <code>QCompleter</code> to a <code>QLineEdit</code>.</p>
<p dir="auto"><strong>Documentation</strong></p>
<p dir="auto">I have read documentation at <a href="http://doc.qt.io/qt-5/qcompleter.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qcompleter.html</a> and <a href="http://doc.qt.io/qt-5/qlineedit.html#setCompleter" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qlineedit.html#setCompleter</a> carefully.  Taking an absolute default code:</p>
<pre><code>self.le = QtWidgets.QLineEdit()
self.completer = QtWidgets.QCompleter(["111", "112", "121", "2222", "333"])
self.le.setCompleter(self.completer)
</code></pre>
<p dir="auto">When the user clicks (or presses <strong>Enter</strong>) on an item in the popup, the text of the item is copied into the <code>QLineEdit</code>.</p>
<p dir="auto"><em>Can anyone point me to anywhere in the documentation where this is actually stated??</em></p>
<p dir="auto">The only mention I can find of anything related is in <a href="http://doc.qt.io/qt-5/qcompleter.html#activated" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qcompleter.html#activated</a> :</p>
<blockquote>
<p dir="auto">This signal is sent when an item in the popup() is activated by the user (by clicking or pressing return).</p>
</blockquote>
<p dir="auto">Well, I'm not connecting the <code>activated</code> signal.  Is this what it does for a <code>QLineEdit</code> by default or something?</p>
<p dir="auto"><strong>Behaviour</strong></p>
<p dir="auto">My actual objective is: <em>after</em> the selected item has been copied, I want the <code>QLineEdit</code> to treat it as the use has finished editing the text, so that it gets acted on immediately.  (I would have thought this is a not-uncommon requirement.  It's the kind of behaviour you'd expect if you were using an (editable) combobox.)</p>
<p dir="auto">I have come up with the following addition to the above code, which seems to work:</p>
<pre><code>self.le.completer().activated.connect(self.le.editingFinished.emit)
</code></pre>
<p dir="auto">Is this right/best?  My connecting the <code>activated</code> signal does not seem to have stopped selecting an item from copying it to the <code>QLineEdit</code>, so maybe it never used the <code>activated</code> signal itself to do that in the first place?  What is doing the text copy when?  Am I supposed to call the base <code>QCompleter::activated</code> slot before/after my handler too to do anything more?</p>
]]></description><link>https://forum.qt.io/topic/94171/qcompleter-for-qlineedit-documentation-and-behaviour-question</link><generator>RSS for Node</generator><lastBuildDate>Wed, 11 Mar 2026 00:21:31 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/94171.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Aug 2018 14:53:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Fri, 31 Aug 2018 09:07:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gojir4">@<bdi>Gojir4</bdi></a><br />
This is generic code for making the <code>QCompleter</code> work (i.e. accept picking a suggestion as meaning we have edited the line edit, finished editing it, and want it to be acted on immediately if appropriate) against <em>any</em> <code>QLineEdit</code> it's attached to.  Imagine this was functionality that Qt had decided to provide in their base, generic <code>QCompleter</code>.  How would they have coded it?  They cannot possibly know what/whether the <code>QLineEdit</code> has a specific function attached to it, can they?</p>
<p dir="auto">The <em>whole point</em> of Qt's signal/slot mechanism is that objects/widgets emit signals and leave the outside world to connect those to slots as appropriate if desired, and that logic/behaviour (whether slots are attached and if so what they do), so that the signal emitter is isolated from what might be going on in the slot receiver(s).</p>
]]></description><link>https://forum.qt.io/post/479028</link><guid isPermaLink="true">https://forum.qt.io/post/479028</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 31 Aug 2018 09:07:04 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Fri, 31 Aug 2018 08:59:06 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/479006">QCompleter for QLineEdit documentation and behaviour question</a>:</p>
<blockquote>
<p dir="auto">I'll try one more time.  I'm not sure whether you're trying to say something I don't understand or whether we are talking at cross-purposes :)</p>
</blockquote>
<p dir="auto">hmmm... I'm not sure too :)</p>
<p dir="auto">What I'm trying to say is that connecting <code>activated</code> signal to the <code>editFinished</code> one or directly to your "outside world" slot (which refresh the page) will have the same result.</p>
<p dir="auto">But if you can't accessing the slot to make the connect , so in this case I agree that you need to connect to <code>editFinished</code> .</p>
]]></description><link>https://forum.qt.io/post/479026</link><guid isPermaLink="true">https://forum.qt.io/post/479026</guid><dc:creator><![CDATA[Gojir4]]></dc:creator><pubDate>Fri, 31 Aug 2018 08:59:06 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Fri, 31 Aug 2018 07:21:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gojir4">@<bdi>Gojir4</bdi></a><br />
I'll try one more time.  I'm not sure whether you're trying to say something I don't understand or whether we are talking at cross-purposes :)</p>
<ul>
<li>
<p dir="auto">I wish to add a <code>QCompleter</code> to some <code>QLineEdit</code>s I already have.</p>
</li>
<li>
<p dir="auto">Those <code>QLineEdit</code>s <em>may</em> already be set up so that when editing completed (e.g. press <strong>Enter</strong> or perhaps press <strong>Tab</strong> to move off) code wishes to act on that immediately.  (In the current particular situation this is indeed the case: when the user has finished typing into the <code>QLineEdit</code> its content is a "filter" which immediately causes refresh of the whole page.)</p>
</li>
<li>
<p dir="auto">The outside world will have hooked to a signal from the <code>QLineEdit</code> to accomplish this behaviour.  The outside world will know nothing about whether the <code>QLineEdit</code> does or does not have a <code>QCompleter</code> attached.</p>
</li>
<li>
<p dir="auto">When the user clicks to accept a suggestion, the <code>QCompleter</code> appears to first copy the text into the <code>QLineEdit</code> (that emits <code>textChanged</code>) and then  explicitly emit the <code>activated</code> signal.</p>
</li>
<li>
<p dir="auto">I can't use the <code>textChanged</code> signal as that is emitted as text is being typed in, that's too early to act on since we're waiting for the complete text before we can act on it.</p>
</li>
</ul>
<p dir="auto">In your example, <em>you have written your own <code>onEditFinished()</code> slot and connected <code>QCompleter::activated</code> signal to it</em>.  However, that is not the real world.  The outside world will (or may) have connected a function to <code>QLineEdit::editingFinished</code> for whatever behaviour it wants on editing completed.  <em>In the <code>QCompleter</code> we know nothing about that, so we don't have an explicit function to call.</em>  All we know is we'd like the <code>QLineEdit</code> to behave however it would behave if the user had typed into it and finished typing, e.g. by pressing <strong>Enter</strong> or <strong>Tab</strong> or by clicking elsewhere.  That's what my question stated:</p>
<blockquote>
<p dir="auto">My actual objective is: <em>after</em> the selected item has been copied, I want the <code>QLineEdit</code> to treat it as [if] the use[r] has finished editing the text, so that it gets acted on immediately.</p>
</blockquote>
<p dir="auto">So, I don't know about you but I cannot see how the <code>QCompleter::activated</code> would know what to call to achieve that, other than by causing the <code>QLineEdit</code> to emit its <code>editingFinished</code> signal?</p>
]]></description><link>https://forum.qt.io/post/479006</link><guid isPermaLink="true">https://forum.qt.io/post/479006</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 31 Aug 2018 07:21:24 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Thu, 30 Aug 2018 21:35:46 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/478943">QCompleter for QLineEdit documentation and behaviour question</a>:</p>
<blockquote>
<p dir="auto">Why wouldn't you connect to editFinished?  Maybe there will indeed be a validator.  All I know is I want selection to be treated as whatever editing finished would do.  I don't know what that might be, so I emit the signal.   What possible disadvantage are you suggesting there might be?</p>
</blockquote>
<p dir="auto">You're right. What I meant is you don't need to connect <code>activated</code> to <code>editFinished</code>. I was assuming that <code>editFinished</code> will be called "automatically" by QLineEdit's slot connected to <code>activated</code> , with a "validated" string as argument, except if validation failed. But I was wrong as it only emits <code>textChanged</code> indirectly by calling <code>QLineEdit::setText()</code>. Also the doc says "<em>The text is not validated when inserted with setText().</em>". So it seems that using completer will bypass the validator anyway.</p>
<p dir="auto">So I think you can assume that the string argument from <code>activated</code> will be the same value than the one contained in the line edit.</p>
<pre><code>def onEditFinished(text): //text contains value selected from completer
    print(le.text()) //Get updated value from QLineEdit
self.le.completer().activated.connect(onEditFinished)
</code></pre>
<p dir="auto">If the goal is to handle the new value, connecting to <code>activated</code> seems enough to me. If the goal is to explicitly emit <code>editFinished</code>, so ok you need to make the "extra" connection but I don't see the benefits.</p>
]]></description><link>https://forum.qt.io/post/478966</link><guid isPermaLink="true">https://forum.qt.io/post/478966</guid><dc:creator><![CDATA[Gojir4]]></dc:creator><pubDate>Thu, 30 Aug 2018 21:35:46 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Thu, 30 Aug 2018 20:19:08 GMT]]></title><description><![CDATA[<p dir="auto">You can request an improvement to the documentation on the <a href="https://bugreports.qt.io" target="_blank" rel="noopener noreferrer nofollow ugc">bug report system</a> or even better, you can submit an improvement to the documentation :)</p>
]]></description><link>https://forum.qt.io/post/478945</link><guid isPermaLink="true">https://forum.qt.io/post/478945</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 30 Aug 2018 20:19:08 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Thu, 30 Aug 2018 20:16:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> , <a class="plugin-mentions-user plugin-mentions-a" href="/user/gojir4">@<bdi>Gojir4</bdi></a><br />
Thank you both.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<blockquote>
<p dir="auto">You don't connect that signal yourself, it's handled internally.<br />
setText is used when you select an entry.</p>
</blockquote>
<p dir="auto">Fair enough.  I don't know if you are the person to mention it to, but that could do with being documented.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gojir4">@<bdi>Gojir4</bdi></a><br />
Don't worry about the Python!  C++ is fine.<br />
The bit I don't get is</p>
<blockquote>
<p dir="auto">So except if you have some validator on your QLineEdit, I would suggest to use the signal activated directly without connecting it to QLineEdit::editFinished().</p>
</blockquote>
<p dir="auto">Why <em>wouldn't</em> you connect to <code>editFinished</code>?  Maybe there will indeed be a validator.  All I know is I want selection to be treated as whatever editing finished would do.  I don't know what that might be, so I emit the signal.   What possible disadvantage are you suggesting there might be?</p>
]]></description><link>https://forum.qt.io/post/478943</link><guid isPermaLink="true">https://forum.qt.io/post/478943</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 30 Aug 2018 20:16:14 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Thu, 30 Aug 2018 19:43:52 GMT]]></title><description><![CDATA[<p dir="auto">Hi ,<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/478904">QCompleter for QLineEdit documentation and behaviour question</a>:</p>
<blockquote>
<p dir="auto">When the user clicks (or presses Enter) on an item in the popup, the text of the item is copied into the QLineEdit.<br />
Can anyone point me to anywhere in the documentation where this is actually stated??</p>
</blockquote>
<p dir="auto">I don't think you will find this in the documentation. This is implicit as this is the goal of the completer. QLineEdit is already designed to handle <code>actived</code> and <code>highlighted</code> signals and to set the text accordingly.<br />
Here is the source code of <code>QLineEdit:setCompleter()</code>:</p>
<pre><code>void QLineEdit::setCompleter(QCompleter *c)
{
    Q_D(QLineEdit);
    if (c == d-&gt;control-&gt;completer())
        return;
    if (d-&gt;control-&gt;completer()) {
        disconnect(d-&gt;control-&gt;completer(), 0, this, 0);
        d-&gt;control-&gt;completer()-&gt;setWidget(0);
        if (d-&gt;control-&gt;completer()-&gt;parent() == this)
            delete d-&gt;control-&gt;completer();
    }
    d-&gt;control-&gt;setCompleter(c);
    if (!c)
        return;
    if (c-&gt;widget() == 0)
        c-&gt;setWidget(this);
    if (hasFocus()) {
        QObject::connect(d-&gt;control-&gt;completer(), SIGNAL(activated(QString)),
                         this, SLOT(setText(QString)));
        QObject::connect(d-&gt;control-&gt;completer(), SIGNAL(highlighted(QString)),
                         this, SLOT(_q_completionHighlighted(QString)));
    }
}
</code></pre>
<p dir="auto">There is two important things which are done here to make it working:</p>
<ul>
<li>Calling <a href="http://doc.qt.io/qt-5/qcompleter.html#setWidget" target="_blank" rel="noopener noreferrer nofollow ugc">QCompleter::setWidget()</a> and pass pointer on the "edit" widget in argument (<code>this</code>).</li>
<li>Connecting to signal <code>actived</code>  to handle insertion of completion item and signal <code>highlighted</code>, which may be used depending of the <a href="http://doc.qt.io/qt-5/qcompleter.html#completionMode-prop" target="_blank" rel="noopener noreferrer nofollow ugc">CompletionMode</a></li>
</ul>
<p dir="auto">So except if you have some validator on your QLineEdit, I would suggest to use the signal <code>activated</code> directly without connecting it to <code>QLineEdit::editFinished()</code>.<br />
I will try to make an example with my bad python level :)</p>
<pre><code>def handleNewText(text):
    print(text)
self.le.completer().activated.connect(handleNewText)
</code></pre>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/478904">QCompleter for QLineEdit documentation and behaviour question</a>:</p>
<blockquote>
<p dir="auto">Is this right/best?  My connecting the activated signal does not seem to have stopped selecting an item from copying it to the QLineEdit, so maybe it never used the activated signal itself to do that in the first place?  What is doing the text copy when?  Am I supposed to call the base QCompleter::activated slot before/after my handler too to do anything more?</p>
</blockquote>
<p dir="auto">Connecting to asignal will not disconnect it from the QLineEdit, so it will not stop completion in your QLineEdit.</p>
]]></description><link>https://forum.qt.io/post/478938</link><guid isPermaLink="true">https://forum.qt.io/post/478938</guid><dc:creator><![CDATA[Gojir4]]></dc:creator><pubDate>Thu, 30 Aug 2018 19:43:52 GMT</pubDate></item><item><title><![CDATA[Reply to QCompleter for QLineEdit documentation and behaviour question on Thu, 30 Aug 2018 19:41:23 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You don't connect that signal yourself, it's handled internally.</p>
<p dir="auto"><code>setText</code> is used when you select an entry.</p>
]]></description><link>https://forum.qt.io/post/478937</link><guid isPermaLink="true">https://forum.qt.io/post/478937</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 30 Aug 2018 19:41:23 GMT</pubDate></item></channel></rss>