Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to preserve TextField binding when user manually edits value

How to preserve TextField binding when user manually edits value

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rolias
    wrote on last edited by
    #1

    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++.

    Check out my third course in the trilogy on Qt
    "Integrating Qt Quick with C++"
    http://bit.ly/qtquickcpp
    published by Pluralsight

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lack
      wrote on last edited by
      #2

      Sorry if I misunderstood you, but isn't this what you wanted?
      @
      ColumnLayout
      {
      property alias fieldValue : fieldID.text

          TextField
          {
              id: fieldID
          }
      }
      

      @

      this way 'fieldValue' property and your textfield's text will always be bound.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rolias
        wrote on last edited by
        #3

        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.

        Check out my third course in the trilogy on Qt
        "Integrating Qt Quick with C++"
        http://bit.ly/qtquickcpp
        published by Pluralsight

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved