How to preserve TextField binding when user manually edits value
-
I'm trying to understand the proper way to use QtQuick with C++. I created some simple QML in a file. I'm winnowing it down for this post, the relevant part looks like this
@
ColumnLayout
{
property string fieldValueTextField { text:fieldValue onEditingFinished: { fieldValue = text } }
}@
This gets used multiple times in the app and the property fieldValue gets bound to different properties defined higher in the QML hierarchy.
I want C++ code to be able to set the value of this field from code and I have no problem doing that. The problem comes when the user clicks on this field in the form and modifies the field directly. When onEditingFinshed() fires I want to update fieldValue because that will eventually emit a signal from the QML that the C++ will receive. The thing I don't understand is that setting fieldValue = text appears to remove the binding between text and fieldValue. I'm not sure why this should be. I could see if I set _text _directly in JavaScript how this would be the case, but I'm just setting the value of the property. I eventually figured out a work-around where I add a signal to this bit of QML and in onEditingFinished() I just raise that signal and pass text as a parameter. I haven't seen this talked about in the docs. Is this how this is supposed to work? Maybe I'm taking the completely wrong approach for modifying a form field both in the form and in C++.
-
Sorry if I misunderstood you, but isn't this what you wanted?
@
ColumnLayout
{
property alias fieldValue : fieldID.textTextField { id: fieldID } }
@
this way 'fieldValue' property and your textfield's text will always be bound.
-
That's exactly what I was looking for. I still don't understand why the binding is lost, but I'm really after the correct way of coding QML and using an alias is much simpler than what I was doing. In fact in my actual code I will create an alias to each top level item (i.e TextField and similar objects) and that way the user of the component can have access to anything they want. Thank you very much.