<?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[Disable TextInput update during editing]]></title><description><![CDATA[<p dir="auto">My <code>TextInput</code> is bound to a C++ function, like this:</p>
<pre><code>TextInput {
    text: MyApp.foo()
}
</code></pre>
<p dir="auto">of course, I need to disable updates when editing. I tried something like this:</p>
<pre><code>Item {
    property bool _dirty: false
    property bool _cancel: false
    property string _oldText

    TextInput {
        color: _dirty ? "red" : "forestgreen"
        text: {
            var value = MyApp.foo()
            if (_dirty) {
                if (parseFloat(value) === parseFloat(text)) _dirty = false
                return text

            } else return value === undefined ? "-" : parseFloat(value).toFixed(2)
        }

        Keys.onEscapePressed: {
            _cancel = true
            focus = false
        }

        onEditingFinished: {
            if (_cancel) {
                text = _oldText
                _oldText = ""
                _dirty = false
                _cancel = false

            } else {
                MyApp.write(text)
                focus = false
            }
        }

        onActiveFocusChanged: {
            if (activeFocus) {
                _oldText = text
                selectAll()
                _dirty = true
            }
        }
    }
}
</code></pre>
<p dir="auto">The expected behavior is the following:</p>
<ol>
<li>normally the text is given by the <code>foo()</code> value, green color</li>
<li>when the user enter in the fields, the color turns red and you can edit the text</li>
<li>if you press 'esc' it ends restoring the previous value and set the green color</li>
<li>if you press 'enter' it calls a C++ functions and waits until the <code>foo()</code> value is equal to the inserted one. When this happens it turns green and binds again with the foo value</li>
</ol>
<p dir="auto">The problem is it complains about a binding loop for property "text".<br />
It doesn't say the exact line but I might guess is:</p>
<pre><code>return text
</code></pre>
<p dir="auto">I thought to keep the current value it would be enough to return the actual one. Is it wrong?</p>
]]></description><link>https://forum.qt.io/topic/102332/disable-textinput-update-during-editing</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 13:18:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/102332.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 01 May 2019 15:01:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Disable TextInput update during editing on Thu, 02 May 2019 10:02:55 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/mark81">@<bdi>Mark81</bdi></a> ,yeah it depends upon how complex things you need to handle, for example if you just need to handle only 2 scenarios like if the value of "myFooVar" is "1", you want to display "Hello" else "Bye" then, you can do like</p>
<pre><code> text: myFooVar === 1 ? "Hello" : "Bye"
</code></pre>
<p dir="auto">I dont think there is a predefined property or a function to retrieve or store the last value.</p>
]]></description><link>https://forum.qt.io/post/526528</link><guid isPermaLink="true">https://forum.qt.io/post/526528</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Thu, 02 May 2019 10:02:55 GMT</pubDate></item><item><title><![CDATA[Reply to Disable TextInput update during editing on Thu, 02 May 2019 09:26:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/shrinidhi-upadhyaya">@<bdi>Shrinidhi-Upadhyaya</bdi></a> said in <a href="/post/526514">Disable TextInput update during editing</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark81">@<bdi>Mark81</bdi></a> , you mean the current text right?It should be a string :-)</p>
</blockquote>
<p dir="auto">I mean, if you have a <code>Text</code> object you can set its <code>text</code> property to any value in two ways:</p>
<pre><code>text: myTextVar
</code></pre>
<p dir="auto">or</p>
<pre><code>text: { return myTextVar }
</code></pre>
<p dir="auto">the second way allows you do to more complex things, i.e.:</p>
<pre><code>text: {
    if (myFooVar === 1) return "blabla"
    else if (myFoo2Var === 10) return myTextVar1
    else return myTextVar2
}
</code></pre>
<p dir="auto">but sometime you might need to leave the value unchanged, I mean keeping the last value. I'm aware I can do it using another variable but it become clumsy:</p>
<pre><code>text: {
    var currText
    if (myFooVar === 1) currText = "blabla"
    else if (myFoo2Var === 10) currText = myTextVar1
    else currText = oldText // keep the current content

    oldText = currText
    return currText
}
</code></pre>
<p dir="auto">I though it was fine and more readable to return the actual value:</p>
<pre><code>text: return text
</code></pre>
<p dir="auto">but it creates a binding-loop error (I hoped this case was handled as an exception in order to keep the current value).<br />
So I wonder if Qt developer provided a way to keep the current value or we need to use another variable like the example above.</p>
]]></description><link>https://forum.qt.io/post/526518</link><guid isPermaLink="true">https://forum.qt.io/post/526518</guid><dc:creator><![CDATA[Mark81]]></dc:creator><pubDate>Thu, 02 May 2019 09:26:35 GMT</pubDate></item><item><title><![CDATA[Reply to Disable TextInput update during editing on Thu, 02 May 2019 09:19:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark81">@<bdi>Mark81</bdi></a> , you mean the current text right?It should be a string :-)</p>
]]></description><link>https://forum.qt.io/post/526514</link><guid isPermaLink="true">https://forum.qt.io/post/526514</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Thu, 02 May 2019 09:19:20 GMT</pubDate></item><item><title><![CDATA[Reply to Disable TextInput update during editing on Thu, 02 May 2019 09:15:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/shrinidhi-upadhyaya">@<bdi>Shrinidhi-Upadhyaya</bdi></a> thanks for your hints! Out of curiosity, what should be the return value of a property to maintain the current value?</p>
]]></description><link>https://forum.qt.io/post/526513</link><guid isPermaLink="true">https://forum.qt.io/post/526513</guid><dc:creator><![CDATA[Mark81]]></dc:creator><pubDate>Thu, 02 May 2019 09:15:21 GMT</pubDate></item><item><title><![CDATA[Reply to Disable TextInput update during editing on Thu, 02 May 2019 05:44:40 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/mark81">@<bdi>Mark81</bdi></a> , here is the sample code:-</p>
<pre><code>Rectangle {
        id: root

        height: 100
        width: 100
        color: "transparent"
        border.color: "red"

        property string oldTxt: 'Hello World'

        TextInput {
            id: inpTxt

            color: activeFocus ? "red" : "forestgreen"
            anchors.fill: parent

            Keys.onEscapePressed: {
                focus = false
                root.foo('')
            }

            Keys.onReturnPressed: {
                root.oldTxt = inpTxt.text
                focus = false
                root.foo(text)
            }
        }

        function foo(newText) {
            if(newText === '')
                inpTxt.text = root.oldTxt
            else
                inpTxt.text = newText
        }

        Component.onCompleted: {
            root.foo('')
        }
    }
</code></pre>
<p dir="auto">Note:- as its just a sample code, i have called the javascript function, you need to call the c++ function as you are doing it now.</p>
]]></description><link>https://forum.qt.io/post/526478</link><guid isPermaLink="true">https://forum.qt.io/post/526478</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Thu, 02 May 2019 05:44:40 GMT</pubDate></item></channel></rss>