c++qtqset

How to modify elements of a QSet?


I have QSet<QuadPattern> texture; and I would like modify all QuadPattern in a loop.

foreach is not the good solution because it makes a copy.

With this code :

QMutableSetIterator<QuadPattern>  it(texture);
while (it.hasNext())
{
    it.next();
    it.value().setCoordinate(...));
}

I get a compilation error :

error: C2662: 'QuadPattern::setCoordinate' : cannot convert 'this' pointer from 'const QuadPattern' to 'QuadPattern &'
Conversion loses qualifiers

The function setCoordinate is like this :

inline void setCoordinate(const std::pair &value) { _coordinate = value; }

Why this error ?


Solution

  • Error is because you try to modify the const object returned by the QMutableSetIterator::value() function.

    I believe you cannot modify your set's items in that way, and therefore there is no such API. What you need to modify all items is simply clearing the set and inserting new items again.