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. Q_FOREACH no longer works as intended
Forum Updated to NodeBB v4.3 + New Features

Q_FOREACH no longer works as intended

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 768 Views 2 Watching
  • 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.
  • J Offline
    J Offline
    jamr
    wrote on last edited by
    #1

    For some reason Q_FOREACH will only enter the loop once. It has previously (a few months ago) not been a problem with the same code and the same setup.

    This is an issue when I use Linux with:

    • gcc
    • Qt 5.1.1

    However not when I use Windows with:

    • msvc2012
    • Qt 5.1.1

    An example of a Q_FOREACH with this exact behaviour:

    QMap<QString, QString> map;
    
    map["KeyOne"] = QStringLiteral("ValueOne");
    map["KeyTwo"] = QStringLiteral("ValueTwo");
    
    Q_FOREACH(QString key, map.keys()) {
        // Will only enter once, with KeyOne
    }
    
    JonBJ 1 Reply Last reply
    1
    • J jamr

      For some reason Q_FOREACH will only enter the loop once. It has previously (a few months ago) not been a problem with the same code and the same setup.

      This is an issue when I use Linux with:

      • gcc
      • Qt 5.1.1

      However not when I use Windows with:

      • msvc2012
      • Qt 5.1.1

      An example of a Q_FOREACH with this exact behaviour:

      QMap<QString, QString> map;
      
      map["KeyOne"] = QStringLiteral("ValueOne");
      map["KeyTwo"] = QStringLiteral("ValueTwo");
      
      Q_FOREACH(QString key, map.keys()) {
          // Will only enter once, with KeyOne
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @jamr
      I don't know the answer to why you say you see a change in behaviour (different gcc version??). You might like to read https://stackoverflow.com/questions/10522155/how-does-q-foreach-foreach-macro-work-and-why-is-it-that-complex for detailed discussion as to what it does under gcc vs msvc. As described in, say, https://www.kdab.com/goodbye-q_foreach/, Q_FOREACH has become deprecated. Maybe it's time to consider moving on as suggested there and elsewhere?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jamr
        wrote on last edited by
        #3

        Yes, rewriting with regular for-loops would be an option. C++03 is used so I cant really use most of the suggestions there.

        Pl45m4P 1 Reply Last reply
        0
        • J jamr

          Yes, rewriting with regular for-loops would be an option. C++03 is used so I cant really use most of the suggestions there.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @jamr

          C++03?! Are you on Win XP as well? ;)
          Time to upgrade ;)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1

          • Login

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