Porting QList iteration to Qt4
From ArkWiki
While porting QtParted from Qt3 to Qt4, I realised that QtParted uses the following QList iteration construct extensively:
QList <ClassName*> objlist;
// populate object list
ClassName* obj = NULL;
for (obj = (ClassName *)objlist.first(); obj; obj = (ClassName *)objlist.next()) {
//do something with obj
}
Unfortunately in Qt4, QList no longer provides first() and next method. A more direct translation to Qt4 for the above scenario would be:
QList <ClassName*> objlist;
// populate object list
ClassName* obj = NULL;
QListIterator <ClassName*> objiter(objlist);
while(objiter.hasNext())
{
obj = objiter.next();
//do something with obj;
}
But I prefer to do the following:
QList <ClassName*> objlist;
// populate object list
foreach (ClassName* obj, objlist) {
//do something with obj
}
It will work either way and that's all folks. Happy QT!!!
--Deux 03:37, 27 February 2008 (UTC)
