Sijith
Sijith

Reputation: 3922

Remove an item from QList throws error

How to delete an object form Qlist

QList<CascadeJobInfo> m_InternJobInfoList;
foreach (CascadeJobInfo jobInfo, m_InternJobInfoList)
{

    m_InternJobInfoList.removeOne(jobInfo);
}

it throws error C:\Qt\Qt5.7.0\5.7\mingw53_32\include\QtCore\qlist.h:972: error: no match for 'operator==' (operand types are 'CascadeJobInfo' and 'const CascadeJobInfo') if (n->t() == t) ^

Upvotes: 0

Views: 4326

Answers (2)

You're not asking how to delete "an" object from a list, but how to remove all objects. Use clear():

m_InternJobInfoList.clear();

If you're asking how to remove only objects for which some predicate is true, you'd want to use erase instead:

auto & list = m_InternJobInfoList;
auto const pred = [](const CascadeJobInfo &){ return true; };
list.erase(std::remove_if(list.begin(), list.end(), pred), list.end());

Of course pred could do something more useful.

In the future C++ and Qt, hopefully you will be able to simply do

erase_if(list, pred);

Upvotes: 2

Dmitry
Dmitry

Reputation: 3143

You need to implement operator== for type CascadeJobInfo:

class CascadeJobInfo
{
public:
    <...>
    bool operator==(const CascadeJobInfo & other) const;
    <...>
};

bool CascadeJobInfo::operator==(const CascadeJobInfo & other) const
{
    if (this == &other) {
        return true;
    }

    bool equal = <...compare each data member within this object with its counterpart in other...>;
    return equal;
}

The official documentation says that pretty clear:

This function requires the value type to have an implementation of operator==().

Also, it is not clear what you are trying to achieve from your code snippet. Attempting to remove each list's item while iterating over the list has a simpler alternative: method clear().

Upvotes: 2

Related Questions