I'm trying to add some features to an older Qt4 application, and I'm new to Qt. The application uses the foreach
keyword which I believe is implemented by Qt. However all foreach
loops in the application only run once, regardless of the number of items that are in the container.
I added this sanity check to the application:
QString test("1234");
int i = 0;
foreach (QChar c, test) {
i++;
}
int stl = 0;
for (QString::iterator j = test.begin(); j != test.end(); j++) {
stl++;
}
qDebug()
<< "string:" << test
<< "size:" << test.size()
<< "foreach:" << i
<< "stl:" << stl
;
It always shows this message:
string: "1234" size: 4 foreach: 1 stl: 4
I've tested it with the above QString
and with a QModelIndexList
and each time it only runs the loop once, even when the container reports having more than one item, and in both cases the STL-style loop works fine, it's only the foreach
that exits the loop early.
What am I doing wrong? The application is built against Qt 4.8.7.
For the record, it turns out this is a change in behaviour with GCC 9 (bug report) to do with where break;
statements should appear and what they do.
It seems GCC versions before 9 did the wrong thing, but Qt 4 was written around that behaviour, so once it was fixed in GCC 9, Qt's foreach looping broke.
It looks like it has been addressed in recent Qt versions but not unfortunately in Qt 4.