<?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[Issue with TextField in ListView delegate]]></title><description><![CDATA[<p dir="auto">I have implemented a 'property table' using <code>ListView</code>, with a <code>TextField</code> in the delegate implementation for value input. The idea is that a value should be committed on pressing enter or on the text field losing focus. To this end, I am using an <code>onEditingFinished</code> handler in the <code>TextField</code>.</p>
<p dir="auto">I am relying on the <code>index</code> property in the delegate to tell me which item in the table has been edited. That is needed to allow me to make an appropriate commit to my underlying model.</p>
<p dir="auto">The issue I am seeing is that in some circumstances, when the text field loses focus and <code>onEditingFinished</code> is triggered, the <code>index</code> is -1. This means that although I might know that the value has changed and that it needs to be committed, I don't have the information about which item it actually is.</p>
<p dir="auto">Whether this happens seems to depend on exactly where in the application focus goes to. For example, the property table is below a tree and if I click on a tree branch the problem does not seem to happen. On the other hand if I click on another list that in a part of the application that is 'further away', it does happen.</p>
<p dir="auto">Because this is happening in a relatively complex scenario, it is quite difficult to narrow it down to a simple example. For now, I wondered if anyone recognised the behaviour where the <code>index</code> can be -1 in a <code>ListView</code> delegate or if anyone thought what I am trying to do is fundamentally wrong for some reason.</p>
<p dir="auto">I am wondering about using the <code>textEdited</code> signal. I do <em>not</em> want to commit on every single edit as this would be too expensive. However I could use <code>textEdited</code> to store a 'pending' value together with  the <code>index</code> (which I assume would be valid in that signal handler). Then I could submit the pending value in <code>onEditingFinished</code>, ignoring the fact that <code>index</code> might be invalid at that point.</p>
]]></description><link>https://forum.qt.io/topic/114261/issue-with-textfield-in-listview-delegate</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 13:51:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114261.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Apr 2020 09:16:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Issue with TextField in ListView delegate on Wed, 13 May 2020 15:29:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> Sorry for delayed response to your suggestion - I was pulled onto other things for a while. Now that I have come back to it a bit more fresh I see that there was something specific happening in my application which probably explains why your solution did not work for me. As I mentioned in my first post, the issue depended on where I clicked in the application. I realise now that it was a result of that click (on an item in another <code>ListView</code>) in turn affecting the current selection in the tree. This then reset the model being shown in the property view. What was a bit surprising to me was that the focus out (<code>onEditingFinished</code>) of my text field happens quite late, after all of this other stuff. Anyway I have found a way to deal with it, along the lines that I was originally thinking about, making more use of <code>onTextEdited</code>.</p>
<p dir="auto">Having said all that, like you, I have definitely seen a more general issue of losing the <code>index</code> value in the past and I suspect your first suggestion would have worked in that case so thank you for taking the time to suggest it.</p>
]]></description><link>https://forum.qt.io/post/594496</link><guid isPermaLink="true">https://forum.qt.io/post/594496</guid><dc:creator><![CDATA[Bob64]]></dc:creator><pubDate>Wed, 13 May 2020 15:29:46 GMT</pubDate></item><item><title><![CDATA[Reply to Issue with TextField in ListView delegate on Fri, 01 May 2020 05:56:57 GMT]]></title><description><![CDATA[<p dir="auto">Weird. I had a very similar case recently and this trick with property worked fine.</p>
<p dir="auto">There is one other possibility - do not <em>bind</em> the new index property, but <em>assign</em> it. So, do this:</p>
<pre><code>MyTextField {
  property int row: -1
  Component.onCompleted: row = index
}
</code></pre>
<p dir="auto">Since you are adding itermediate property in Loader, you can actually do it there, when sourceComponent is changed.</p>
<p dir="auto">This way the <code>row</code> is assigned only once. Even if <code>index</code> changes to <code>-1</code>, <code>row</code> will not be updated - so it should work as you intend it to.</p>
]]></description><link>https://forum.qt.io/post/591938</link><guid isPermaLink="true">https://forum.qt.io/post/591938</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 01 May 2020 05:56:57 GMT</pubDate></item><item><title><![CDATA[Reply to Issue with TextField in ListView delegate on Thu, 30 Apr 2020 11:45:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> Thanks for the suggestion. I thought that would solve it but unfortunately not. The original index is reported as -1 in <code>onEditingFinished</code>.</p>
<p dir="auto">One detail which I unfortunately omitted was that, because I have different types of property in my table and it needs to be initialised dynamically from data, I am using a <code>Loader</code> as a 'delegate factory' . The delegates themselves are defined as <code>Component</code>s such as:</p>
<pre><code>    // delegate for integer type property
    Component {
        id: intFieldDelegate
        MyTextField {
            validator: IntValidator{}
        }
    }
</code></pre>
<p dir="auto">One consequence of this is that I don't have direct access to <code>index</code> etc in my delegate and have to introduce an intermediate property, <code>index_</code> say, in my <code>Loader</code> (this is described at the end of the Detailed Description for <code>Loader</code> in the Qt <a href="https://doc.qt.io/qt-5/qml-qtquick-loader.html" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a>).</p>
<p dir="auto">I have tried introducing a readonly property, as suggested, at various levels - in the <code>Loader</code>, in the <code>Component</code> instantiation above, in the implementation file for <code>MyTextField</code> but all with the same result.</p>
]]></description><link>https://forum.qt.io/post/591779</link><guid isPermaLink="true">https://forum.qt.io/post/591779</guid><dc:creator><![CDATA[Bob64]]></dc:creator><pubDate>Thu, 30 Apr 2020 11:45:21 GMT</pubDate></item><item><title><![CDATA[Reply to Issue with TextField in ListView delegate on Thu, 30 Apr 2020 09:35:46 GMT]]></title><description><![CDATA[<p dir="auto">Add a readonly property in your delegate, like this:</p>
<pre><code>YourDelegate {
  readonly property int row: index
}
</code></pre>
<p dir="auto">And use this in your <code>onEditingFinished</code> handler. It should contain the original value from when the delegate was created.</p>
]]></description><link>https://forum.qt.io/post/591761</link><guid isPermaLink="true">https://forum.qt.io/post/591761</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Thu, 30 Apr 2020 09:35:46 GMT</pubDate></item></channel></rss>