Reacting to values derived from ListModels
-
Suppose I have a
ListModel
which, amongst other fields, contains alabel
.ListModel { id: myModel ListElement { label: ..., ... } }
(Though in my case, I am actually not defining
ListElement
s 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. -
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.