Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to iterate a QList<QMap<QString,QString>> list ?
QtWS25 Last Chance

[Solved] How to iterate a QList<QMap<QString,QString>> list ?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    inejose
    wrote on last edited by
    #1

    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.

    call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        ScottR
        wrote on last edited by
        #3

        @
        typedef QMap<QString, QString> myMapType;

        QList<myMapType> myList;

        foreach(myMapType m, myList)
        {
        // code here
        }
        @

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          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".

          1 Reply Last reply
          0
          • I Offline
            I Offline
            inejose
            wrote on last edited by
            #5

            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
            }@

            call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                inejose
                wrote on last edited by
                #7

                Yes, I know, I used it in an operation where the information storage and order don't matter

                Thanks for the advice.

                call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved