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 fieldValue
TextField
{
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++.
↧