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. Help me code and understand "foreach"
Forum Updated to NodeBB v4.3 + New Features

Help me code and understand "foreach"

Scheduled Pinned Locked Moved Unsolved C++ Gurus
9 Posts 6 Posters 1.2k 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 last edited by
    #1

    Trying to iterate thru "localAdapters" using "foreach".
    I do not fully understand the "foreach" parameters.

        localAdapters = QBluetoothLocalDevice::allDevices();
            int index;
            text = localAdapters.at(index).address().toString();
            // run foreach
            foreach(text,localAdapters)
            {
                qDebug()<< text;
            }
    
    

    Of course my code is wrong ....
    I got way too many errors to analyze ....

    /mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/PROJECTS/BT_JAN16_/CCC_SOURCE/btchat/chat.cpp:222: error: no viable overloaded '='
    chat.cpp:222:9: error: no viable overloaded '='
            foreach(text,localAdapters)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qglobal.h:1126:21: note: expanded from macro 'foreach'
    #    define foreach Q_FOREACH
                        ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qglobal.h:1118:19: note: expanded from macro 'Q_FOREACH'
        for (variable = *_container_.i; _container_.control; _container_.control = 0)
             ~~~~~~~~ ^ ~~~~~~~~~~~~~~
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:270:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QChar' for 1st argument
        QString &operator=(QChar c);
                 ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:271:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QString' for 1st argument
        QString &operator=(const QString &) noexcept;
                 ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:272:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QLatin1String' for 1st argument
        QString &operator=(QLatin1String latin1);
                 ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:274:21: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QString' for 1st argument
        inline QString &operator=(QString &&other) noexcept
                        ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:841:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const char *' for 1st argument
        inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
                                           ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:843:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QByteArray' for 1st argument
        inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
                                           ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:845:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'char' for 1st argument
        inline QT_ASCII_CAST_WARN QString &operator=(char c)
                                           ^
    /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:956:21: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QString::Null' for 1st argument
        inline QString &operator=(const Null &) { *this = QString(); return *this; }
                        ^
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      See your question 23 days ago: https://forum.qt.io/topic/141587/how-to-implement-for-foreach-iteration and the documentation: https://doc.qt.io/qt-6/foreach-keyword.html

      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
      3
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        @AnneRanch said in Help me code and understand "foreach":

        QBluetoothLocalDevice

        Try

        for ( auto const & host_info : QBluetoothLocalDevice::allDevices() ) {
              qDebug()<< host_info.address().toString();
        }
        
        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          Refer back to the source:

          https://en.cppreference.com/w/cpp/language/range-for

          Oh... "foreach" not "range based for" so the closest "standard" thing would be std::for_each, and as used above foreach is purely a Qt thing I believe.

          1 Reply Last reply
          0
          • TomZT Offline
            TomZT Offline
            TomZ
            wrote on last edited by
            #5

            Qts foreach is very old. Please consider using the new C++ language support for a very similar thing;

            for (auto item : container) {
            }

            You can find more information about that and it will likely be easier to understand when the compiler complains about problems.

            A 1 Reply Last reply
            0
            • TomZT TomZ

              Qts foreach is very old. Please consider using the new C++ language support for a very similar thing;

              for (auto item : container) {
              }

              You can find more information about that and it will likely be easier to understand when the compiler complains about problems.

              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

              @TomZ OK, I got the message... pretty much in same line as "new style connect"...
              or it is not C++ "standard" ?
              I really do not see why QT is inventing any of these macros anyway, obsolete or not. .

              I am little curious about the usage of type "auto".
              Was it / is in not original "C" and I have seldom see it used.
              Or am I mixing it up with "no type" is automatically "int"?

              Christian EhrlicherC 1 Reply Last reply
              0
              • A Anonymous_Banned275

                @TomZ OK, I got the message... pretty much in same line as "new style connect"...
                or it is not C++ "standard" ?
                I really do not see why QT is inventing any of these macros anyway, obsolete or not. .

                I am little curious about the usage of type "auto".
                Was it / is in not original "C" and I have seldom see it used.
                Or am I mixing it up with "no type" is automatically "int"?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @AnneRanch said in Help me code and understand "foreach":

                I really do not see why QT is inventing any of these macros anyway, obsolete or not. .

                They were invented to use new stuff despite having old compilers / no standard yet supported it but it's useful.

                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
                0
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by Kent-Dorfman
                  #8

                  auto in c++ is automatic type determination based on the right side of the expression

                  it is handy as a shorthand for long convoluted iterator types, or things that are deeply embedded and where the return type is not obvious upon casual inspection.

                  auto x="my string"; // yields const char*
                  auto v=1; // yields int
                  auto b=56U; yields unsigned
                  auto i=std::map::begin(); // yields std::map::iterator

                  get it now?

                  1 Reply Last reply
                  1
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    auto existed in C from the start too! It just didn't mean the same as now in C++, and was pointless :)

                    void func()
                    {
                        auto int i;
                    }
                    
                    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