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. Reacting to values derived from ListModels

Reacting to values derived from ListModels

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

    Suppose I have a ListModel which, amongst other fields, contains a label.

    ListModel {
        id: myModel
        ListElement { label: ..., ... }
    }
    

    (Though in my case, I am actually not defining ListElements inline but adding items programmatically.)

    So I am happy with using this model in some sort of view where the individual fields of the model are used in the view, and everything updates nicely.

    However, what if I want to derive a value from the whole model? The case I have in mind is a maximum label length which I need to use in my UI. That maximum needs to be updated when the model updates and I would like to do it declaratively:

    MyComponent {
        propertyThatDependsOnMaxLabelLength: ???
        ...
    }
    

    For example, would something like this work?

    MyComponent {
        function calcMaxLabelLength(model) { ... }
        propertyThatDependsOnMaxLabelLength: calcMaxLabelLength(myModel)
        ...
    }
    

    i.e., would the function reliably be reevaluated whenever there is any change to the model?

    I know I could write a C++ model and I could simply expose an additional property from that. This might be the best way in terms of efficiency but it seems like overkill for a case where a simple ListModel is otherwise sufficient.

    B 1 Reply Last reply
    0
    • B Bob64

      Suppose I have a ListModel which, amongst other fields, contains a label.

      ListModel {
          id: myModel
          ListElement { label: ..., ... }
      }
      

      (Though in my case, I am actually not defining ListElements inline but adding items programmatically.)

      So I am happy with using this model in some sort of view where the individual fields of the model are used in the view, and everything updates nicely.

      However, what if I want to derive a value from the whole model? The case I have in mind is a maximum label length which I need to use in my UI. That maximum needs to be updated when the model updates and I would like to do it declaratively:

      MyComponent {
          propertyThatDependsOnMaxLabelLength: ???
          ...
      }
      

      For example, would something like this work?

      MyComponent {
          function calcMaxLabelLength(model) { ... }
          propertyThatDependsOnMaxLabelLength: calcMaxLabelLength(myModel)
          ...
      }
      

      i.e., would the function reliably be reevaluated whenever there is any change to the model?

      I know I could write a C++ model and I could simply expose an additional property from that. This might be the best way in terms of efficiency but it seems like overkill for a case where a simple ListModel is otherwise sufficient.

      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      To partially answer my own question: based on a small test app, it certainly seems that the sort of function that I described does in practice get called whenever there is a change to the model.

      Part of me thinks this maybe is obvious based on how things tend to work in QML but I would still feel happier to see it in black and white somewhere.

      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