How do I find a particular ListElement inside a ListModel in qml?

11,683

You will have to iterate and check the list elements.

It is a good idea to write a function if you are going to be performing different searches, where you can pass search criteria as a functor:

function find(model, criteria) {
  for(var i = 0; i < model.count; ++i) if (criteria(model.get(i))) return model.get(i)
  return null
}

Which you can use like this, to find an item with the name of "Banana":

find(fruitModel, function(item) { return item.name === "Banana" })

If multiple hits are possible, you should return an array of items instead of a single item.

Share:
11,683
Aras
Author by

Aras

I am into building web application these days. I tend to work on the frontend where I can combine my love for great user experience with programming.

Updated on June 12, 2022

Comments

  • Aras
    Aras about 2 years

    Qt QML has a ListModel that can be used to represent list data. It provide a number of methods for adding elements to the list but the only method I can find for retrieving an element is the get() method that expect an index.

    What is I want to check if my ListModel contains a particular element? Is there a way to recieve ListElement object(s) using a key and value?

    For example connsider this list:

    ListModel {
        id: fruitModel
    
        ListElement {
            name: "Apple"
            cost: 2.45
        }
        ListElement {
            name: "Orange"
            cost: 3.25
        }
        ListElement {
            name: "Banana"
            cost: 1.95
        }
    }
    

    Is there a method such as fruitModel.get({name: "Banana"}) that would find elements with the name "Banana" and return them?

  • QtRoS
    QtRoS over 7 years
    But this method returns only one element, not all.
  • dtech
    dtech over 7 years
    @QtRoS - read the last paragraph of the answer. The OP was about a particular element, not particular elements. Modifying the code to return an array of hits is trivial.
  • QtRoS
    QtRoS over 7 years
    I don't think that it is trivial for a guy who was asking much simpler solution in original question. Returning array of items is a very bad idea anyway - what if he will store them somewhere? QML runtime says that it is not safe safe to store references returned by model.get(). So in my opinion simple cycle will solve his problem. In more advanced scenario he may consider to use copies of model items.
  • dtech
    dtech over 7 years
    I don't understand the reason for your nitpick, if you have a better answer then go for it. Quite obviously, if you have a reference to an object and then remove the object, you will have a bad time. It doesn't mean that you should never use model item references, it just means to not be stupid about it. Lastly, you could just as easily mod the solution to also include a functor with what to do with each hit in place without ever returning anything. Finally, copies won't do you any good if you intend to make changes to the model items.
  • Kenn Sebesta
    Kenn Sebesta over 2 years
    @dtech FWIW, I appreciate QtRoS's nitpick. It helps me understand better the bounds of your answer, and the ways I could unwittingly make mistakes.