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. Instantiate textInput from C++, and allow user to change value

Instantiate textInput from C++, and allow user to change value

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 332 Views
  • 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.
  • E Offline
    E Offline
    EaccB
    wrote on last edited by EaccB
    #1

    I have a text input defined in the following way:

    TextInput { text: employee.age; 
                font.weight: Font.DemiBold }
    

    'age' is a getter bound to my employee object using Q_PROPERTY.
    Currently, as I would expect, the text input's text reflects the value of age in my employee object, and updates accordingly, when the value is changed on the C++ side.

    However, I now want the user to be able to change that value from the text input, and have it signal to the employee object that the text has changed. The value should then update in the employee object.

    Essentially, I want the employee object to instantiate the value, and then allow the user to alter it, in doing so updating the actual value. How can this be achieved? Cheers.

    EDIT:

    I think I can do what I want using bi-directional property binding (http://imaginativethinking.ca/bi-directional-data-binding-qt-quick/).

    I do have some questions though:

    What's to stop a race condition from happening?
    For example, if the user changes the value in the text input, what's to say that the text won't revert back to it's original value, before it's had a chance to update the value in the employee object?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Darta
      wrote on last edited by Darta
      #2

      When you implemented the Q_PROPERTY, if you put a "WRITE" with a setter to your age you just have to use it:

      TextInput
      {
          text: employee.age; 
          font.weight: Font.DemiBold 
          onTextChanged: employee.setAge(text);
      }
      

      something like that and if you don't add it:

      Q_PROPERTY(int nbLanguage READ nbLanguage WRITE setNbLanguage NOTIFY nbLanguageChanged)
      

      the setter will automaticaly call the NOTIFY signal nbLanguageChanged to change it into the cpp

      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