Nominate our 2022 Qt Champions!

Force property binding to refresh

  • Hello,

    I am new to QML, it's a nice addition to Qt.

    I have several Elements showing data from a XmlListModel, like this :

    id: myPage

    property XmlListModel model : XmlListModel{}
     Text {
          id: title
          text: model.get(0) ? model.get(0).title : ""
     //.... other elements


    Problem: when the model update its content (after a reload() for exemple), the binded properties do not get updated whith the new data. From my understanding of QML, it should be the case, isn't it ?

    For exemple in the exemple above, the text of the title element do not show the new content in the model.
    Is there a way to force the binding to refresh ?

    Maybe this is happening because I call a function in my binding (model.get(0)), is it allowed/supported?
    How would you solve the problem ?


  • Does the model contain only one item? Maybe you can use for example Repeater and set model to XmlListModel.

  • Hi,

    Yes, the issue in this case is the use of get() (C++ functions don't have any way to NOTIFY when the value returned is out-of-date, the way properties do).

    It's not very nice, but you can include a dummy property in the binding that can be changed to force the binding to update. For example, in the snipped below, you could change the value of dummyValue (a bool property that you declare) whenever the model is updated to force the binding to be reevaluated.

    @text: (model.get(0) && (dummyValue || !dummyValue)) ? model.get(0).title : ""@


Log in to reply