I'm new to Qt and trying to learn the idioms.
The foreach
documentation says:
Qt automatically takes a copy of the container when it enters a foreach loop. If you modify the container as you are iterating, that won't affect the loop.
But it doesn't say how to remove an element while iterating with foreach
. My best guess is something like:
int idx = 0;
foreach (const Foo &foo, fooList) {
if (bad(foo)) {
fooList.removeAt(idx);
}
++idx;
}
Seems ugly to have to scope idx
outside the loop (and to have to maintain a separate loop counter at all).
Also, I know that Yes, deep copy happens.foreach
makes a copy of the QList
, which is cheap, but what happens once I remove an element -- is that still cheap or is there an expensive copy-on-modify going on?
EDIT : This doesn't seem like idiomatic Qt either.
for (int idx = 0; idx < fooList.size(); ) {
const Foo &foo = fooList[idx];
if (bad(foo)) {
fooList.removeAt(idx);
}
else ++idx;
}
foreach
for this? – Psychoactiveforeach
, it just seems nice to use, and the docs seemed to suggest there was some way to do it. Qt seems so well thought out, I figured there'd be some obvious idiom for what I'm trying to do. – Neville