Search code examples
c++qtqlist

return items from QList<MyObject> through text string


I need to do finder in my mobile application. So I've this object:

class Category
{
    public:
    Category();

    QString title;
    QString description;

};

on application start i load from json url some objects and i put they in a QList list;

I've done one method that update a model when i click FIND on my gui.

    void CategoryModel::searchByTextInCategoryList(QString testo)
    {
        QList<Category> lista = singleton::instance().categoryCompleteList;
        auto itObj = std::find_if(lista.begin(), lista.end(), [](Category o) { return o.title == "my searched text"; }); 
        //this not for me
    }

I need a similar solutiion, if possible:

QList<Category> result = lista.find_all.where(lista.at(index).title == "search text");

exist this possibility ?

This method don't work really fine for me, because I need to get all objects that contains the same word. Could help me ? I'm came from C#, and in C# I used Linq, there is similar linq in QT to search in Qlist by text ?

In few words...i need to do a query in QList and return multiple items from list.

Thanks


Solution

  • How about a good old foreach ?

    void CategoryModel::searchByTextInCategoryList(QString testo)
    {
        QList<Category> lista = singleton::instance().categoryCompleteList;
        QList<Category> results;
        foreach(Category c, lista) { 
            if (c.title.contains("my searched text"))
            //if (c.title. == "my searched text")
                results.append(c);
        }
    }