Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to iterate QList ?
Forum Updated to NodeBB v4.3 + New Features

How to iterate QList ?

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 4 Posters 1.3k 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 2 May 2024, 15:33 last edited by
    #1

    I am not too comfortable using "foreach" ...... however, is there something very basic wrong with my code ?

    Or am I missing more ?

    define foreach Q_FOREACH

        QList<QObject*>  pLIST = this->children();
        QObject item;
    
    //#ifdef BYPASS
        foreach(auto &pLIST, item )
        {
            text = " Iterate .............  ";
    
            qDebug().noquote() << text;
        }
    

    One of the errors posted

    /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1085: error: no type named 'const_iterator' in 'QObject'
    In file included from settingsdialog.cpp:55:
    In file included from ./mainwindow_Bluetooth.h:61:
    In file included from ../CCC_SOURCE/A_BT_TEST_A/../dialog_connection.h:4:
    In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/QDialog:1:
    In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/qdialog.h:43:
    In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/qtwidgetsglobal.h:43:
    In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtGui/qtguiglobal.h:43:
    /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1085:17: error: no type named 'const_iterator' in 'QObject'
        typename T::const_iterator i, e;
        ~~~~~~~~~~~~^~~~~~~~~~~~~~
    settingsdialog.cpp:1241:5: note: in instantiation of template class 'QtPrivate::QForeachContainer<QObject>' requested here
        foreach(auto &pLIST, item )
        ^
    /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1126:21: note: expanded from macro 'foreach'
    #    define foreach Q_FOREACH
                        ^
    /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1115:25: note: expanded from macro 'Q_FOREACH'
    for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \
                            ^
    
    J 1 Reply Last reply 2 May 2024, 17:15
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 2 May 2024, 15:39 last edited by
      #2

      foreach(auto &pLIST, item )

      item is not a QList

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Anonymous_Banned275
        2 May 2024, 15:33

        I am not too comfortable using "foreach" ...... however, is there something very basic wrong with my code ?

        Or am I missing more ?

        define foreach Q_FOREACH

            QList<QObject*>  pLIST = this->children();
            QObject item;
        
        //#ifdef BYPASS
            foreach(auto &pLIST, item )
            {
                text = " Iterate .............  ";
        
                qDebug().noquote() << text;
            }
        

        One of the errors posted

        /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1085: error: no type named 'const_iterator' in 'QObject'
        In file included from settingsdialog.cpp:55:
        In file included from ./mainwindow_Bluetooth.h:61:
        In file included from ../CCC_SOURCE/A_BT_TEST_A/../dialog_connection.h:4:
        In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/QDialog:1:
        In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/qdialog.h:43:
        In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtWidgets/qtwidgetsglobal.h:43:
        In file included from /home/nov25-1/Qt/5.15.2/gcc_64/include/QtGui/qtguiglobal.h:43:
        /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1085:17: error: no type named 'const_iterator' in 'QObject'
            typename T::const_iterator i, e;
            ~~~~~~~~~~~~^~~~~~~~~~~~~~
        settingsdialog.cpp:1241:5: note: in instantiation of template class 'QtPrivate::QForeachContainer<QObject>' requested here
            foreach(auto &pLIST, item )
            ^
        /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1126:21: note: expanded from macro 'foreach'
        #    define foreach Q_FOREACH
                            ^
        /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qglobal.h:1115:25: note: expanded from macro 'Q_FOREACH'
        for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \
                                ^
        
        J Offline
        J Offline
        JoeCFD
        wrote on 2 May 2024, 17:15 last edited by JoeCFD 5 Mar 2024, 13:52
        #3

        @AnneRanch said in How to iterate QList ?:

        foreach(auto &pLIST, item )
        {
            text = " Iterate .............  ";
        
            qDebug().noquote() << text;
        }
        
            for( auto const list_item : pLIST )
            {
                text = " Iterate .............  ";
                qDebug().noquote() << text;
            }
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on 2 May 2024, 18:06 last edited by
          #4

          Thank you, I am not sure why I cannot get the parameters in correct order...
          Mental block, I guss.

          One more question :
          how important is to designate the item as "const" ?

          J 1 Reply Last reply 2 May 2024, 20:12
          0
          • A Anonymous_Banned275
            2 May 2024, 18:06

            Thank you, I am not sure why I cannot get the parameters in correct order...
            Mental block, I guss.

            One more question :
            how important is to designate the item as "const" ?

            J Offline
            J Offline
            JoeCFD
            wrote on 2 May 2024, 20:12 last edited by JoeCFD 5 Feb 2024, 20:15
            #5

            @AnneRanch my own habit. If list_item is not changed in the loop, you can add a const to enforce it. If anything of list_item is changed by accident, the compiler will tell it is wrong. But maybe no big deal in your app. Up to you.

            1 Reply Last reply
            3
            • J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 3 May 2024, 06:13 last edited by
              #6

              I would recommend to no longer use Q_FOREACH it is deprecated for a reason! Use the ranged based for loop the c++ standard offers
              for (auto &item : container)

              less error prone, less conflict with other libraries, faster compile times.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              5
              • A Anonymous_Banned275 has marked this topic as solved on 3 May 2024, 19:58

              1/6

              2 May 2024, 15:33

              • Login

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