Important: Please read the Qt Code of Conduct -

Binding a TextField to the current TableView item [solved]

  • Hi,

    I'm using the QML desktop-components and want to bind the currently selected row of a TableView to a TextField, so I'm using this code:
    @TextField {
    text: mymodel.get(tableview.currentIndex).myproperty

    If I change the current item in the TableView, the value of the TextField changes, as expected.
    But if I modify the TextField, the model data does not change! In general, I'd think, that this is possible, because QML uses bindings.
    I think the problem is using a getter here.

    Would it be possible to modify this code to something like
    text: mymodel[tableview.currentIndex].myproperty
    to create a bidirectional binding: so that the model is modified, if the value of the TextField is changed?

  • AFAIK, ListModel doesn't support the behavior you need. If you still want to do it you'll have to monitor the change on TextField and call setProperty() on the model, whenever TextField changes.

  • @text: mymodel.get(tableview.currentIndex).myproperty@

    This creates one directional binding. When ever value of right side expressing changes, value of left side variable is updated with new value. But the other way is not true. When ever text changes, it does not updated model data.

  • Ok, thanks.
    I added this line, and now it works:
    @onTextChanged: myModel.setProperty(tableview.currentIndex, "myproperty", text)

  • Good to hear that. Glad I could help.

Log in to reply