Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?



  • 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


Log in to reply