[Solved] How to iterate a QList<QMap<QString,QString>> list ?
-
wrote on 31 Mar 2014, 12:57 last edited by
Hi,
I'd like to iterate a QList<QMap<QString,QString>> list, but with foreach I obtain an error.
I tried this with no result:
@QList<QMap<QString,QString>> myList;
foreach(QMap<QString,QString> singleItem, myList) {
//code here
}@Compiler error are:
C2275 and C2065
Thanks in advance.
-
Please read the documentation of foreach! An explanation an a solution to your problem is already there.
"Link":http://qt-project.org/doc/qt-5/containers.html#the-foreach-keyword.
-
wrote on 31 Mar 2014, 13:10 last edited by
@
typedef QMap<QString, QString> myMapType;QList<myMapType> myList;
foreach(myMapType m, myList)
{
// code here
}
@ -
wrote on 31 Mar 2014, 13:51 last edited by
did you try the native c++11 for-each loop?
it should work without a typedef, just as an alternative if you have access to c++11:
@
QList<QMap<QString,QString>> myList;for(QMap<QString,QString>& singleItem : myList) {
//code here
}
@
I just used a reference (&) for the "singleItem". -
wrote on 3 Apr 2014, 08:45 last edited by
Thank you very much for your responses. I have finally used the following code:
@QList<QMap<QString,QString>> myList;
while(!myList.isEmpty()){
QMap<QString, QString> singleElement = myList.takeFirst();
//my code
}@ -
Hi,
Just to be sure you won't get bitten, you know that takeFirst removes the element ? Are you sure it's the behavior you want ?
-
wrote on 3 Apr 2014, 15:39 last edited by
Yes, I know, I used it in an operation where the information storage and order don't matter
Thanks for the advice.
5/7