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. How to output QList to standard console ?
Forum Update on Monday, May 27th 2025

How to output QList to standard console ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 5.2k 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 24 Oct 2019, 16:08 last edited by Anonymous_Banned275
    #1

    SOLVED

    Here are final tests and they all work fine.
    There is a small issue in debugging these "prints" they get "out of sequence " and if the test "exit" is in wrong place code get missed.

    Thanks for all the wonderful support received in resolving this .

        cout << "***************************************function " << __FUNCTION__ << endl;
        qDebug() << "DEBUG  message " << BT_string_list.size()<< ", says: " << BT_string_list.at(0).name();
        qDebug() << "DEBUG  message " << BT_string_list.size()<< ", says: " << BT_string_list.at(1).name();
        cout << "cout test #1 " << BT_string_list.at(0).name().toStdString() << endl;
        cout <<"cout test #2 " << BT_string_list.at(0).name().toLocal8Bit().constData() << endl;
    

    I am using QList with template QBluetoothHostInfo.
    to retrieve (scan) for local bluetooth devices. It returns correct count of devices, however, I cannot figure out how to retrieve / print the actual names of the devices to console.

    The missing piece is relation between QBluetoothHostInfo and QString .

    I can retrieve single bluetooth device name correctly using QStringList , but need to use QBluetoothHostInfo. to retrieve them all.

    I also like to use standard std::cout to output QString to local console for debugging purposes.
    cout << QString << endl; does not work

    Here is the "work in progress" code

     // buidl a list of local BT devices
       QList<QBluetoothHostInfo> BT_string_list;        // info , not local
       BT_string_list << scan_agent->BTLocalDeviceInfo.allDevices(); // all BT devies
       if(BT_string_list.size())
       {
    #ifdef DEBUG
           cout << endl;
        cout << "TRACE Current marker  " << __FUNCTION__ << endl;
        cout << "BT_string_list.size() OK           @line  list size  " <<( BT_string_list.size()) << "  " << __LINE__ << endl;
       // cout << "function " << __FUNCTION__ << endl;
    #endif
       }
       else
       {
    #ifdef DEBUG
        cout << "TRACE Current marker  " << __FUNCTION__ << endl;
        cout << " BT_string_list.size() FAILED   @line " << BT_string_list.size() << __LINE__ << endl;
        cout << "function " << __FUNCTION__ << endl;
    #endif
       }
       cout << BT_string_list.size() << endl;
    
    P 1 Reply Last reply 24 Oct 2019, 16:55
    0
    • C Offline
      C Offline
      Chrisw01
      wrote on 24 Oct 2019, 16:33 last edited by Chrisw01
      #2

      Have you tried something like this?

      for(int loop1 = 0; loop1 < BT_string_list.size(); loop1++) {
          cout << BT_string_list.at(loop1);
      }
      
      A 1 Reply Last reply 24 Oct 2019, 17:50
      2
      • A Anonymous_Banned275
        24 Oct 2019, 16:08

        SOLVED

        Here are final tests and they all work fine.
        There is a small issue in debugging these "prints" they get "out of sequence " and if the test "exit" is in wrong place code get missed.

        Thanks for all the wonderful support received in resolving this .

            cout << "***************************************function " << __FUNCTION__ << endl;
            qDebug() << "DEBUG  message " << BT_string_list.size()<< ", says: " << BT_string_list.at(0).name();
            qDebug() << "DEBUG  message " << BT_string_list.size()<< ", says: " << BT_string_list.at(1).name();
            cout << "cout test #1 " << BT_string_list.at(0).name().toStdString() << endl;
            cout <<"cout test #2 " << BT_string_list.at(0).name().toLocal8Bit().constData() << endl;
        

        I am using QList with template QBluetoothHostInfo.
        to retrieve (scan) for local bluetooth devices. It returns correct count of devices, however, I cannot figure out how to retrieve / print the actual names of the devices to console.

        The missing piece is relation between QBluetoothHostInfo and QString .

        I can retrieve single bluetooth device name correctly using QStringList , but need to use QBluetoothHostInfo. to retrieve them all.

        I also like to use standard std::cout to output QString to local console for debugging purposes.
        cout << QString << endl; does not work

        Here is the "work in progress" code

         // buidl a list of local BT devices
           QList<QBluetoothHostInfo> BT_string_list;        // info , not local
           BT_string_list << scan_agent->BTLocalDeviceInfo.allDevices(); // all BT devies
           if(BT_string_list.size())
           {
        #ifdef DEBUG
               cout << endl;
            cout << "TRACE Current marker  " << __FUNCTION__ << endl;
            cout << "BT_string_list.size() OK           @line  list size  " <<( BT_string_list.size()) << "  " << __LINE__ << endl;
           // cout << "function " << __FUNCTION__ << endl;
        #endif
           }
           else
           {
        #ifdef DEBUG
            cout << "TRACE Current marker  " << __FUNCTION__ << endl;
            cout << " BT_string_list.size() FAILED   @line " << BT_string_list.size() << __LINE__ << endl;
            cout << "function " << __FUNCTION__ << endl;
        #endif
           }
           cout << BT_string_list.size() << endl;
        
        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 24 Oct 2019, 16:55 last edited by
        #3

        @AnneRanch said in How to output QList to standard console ?:

        I cannot figure out how to retrieve / print the actual names of the devices to console.

        What about QBluetoothHostInfo::name():

        Returns the user visible name of the host info object.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        4
        • C Chrisw01
          24 Oct 2019, 16:33

          Have you tried something like this?

          for(int loop1 = 0; loop1 < BT_string_list.size(); loop1++) {
              cout << BT_string_list.at(loop1);
          }
          
          A Offline
          A Offline
          Anonymous_Banned275
          wrote on 24 Oct 2019, 17:50 last edited by
          #4

          @Chrisw01

          That is the problem - I get this error
          and I do not know how to implement the correct operator <<
          because QBluetoothHostInfo in not a QString .

          /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:97: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const QBluetoothHostInfo')
          cout << BT_string_list.at(loop1);
          ^

          J C 2 Replies Last reply 24 Oct 2019, 17:59
          0
          • A Anonymous_Banned275
            24 Oct 2019, 17:50

            @Chrisw01

            That is the problem - I get this error
            and I do not know how to implement the correct operator <<
            because QBluetoothHostInfo in not a QString .

            /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:97: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const QBluetoothHostInfo')
            cout << BT_string_list.at(loop1);
            ^

            J Offline
            J Offline
            JonB
            wrote on 24 Oct 2019, 17:59 last edited by JonB
            #5

            @AnneRanch
            The object you have there is a whole QBluetoothHostInfo. The error message tells you the << operator hasn't been written to cope with that. You can't convert a whole QBluetoothHostInfo to a QString. @Pablo-J-Rogina is suggesting you output a QBluetoothHostInfo::name(), just the name of it. So presumably

            cout << BT_string_list.at(loop1).name();
            
            A 1 Reply Last reply 24 Oct 2019, 23:54
            3
            • A Anonymous_Banned275
              24 Oct 2019, 17:50

              @Chrisw01

              That is the problem - I get this error
              and I do not know how to implement the correct operator <<
              because QBluetoothHostInfo in not a QString .

              /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:97: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const QBluetoothHostInfo')
              cout << BT_string_list.at(loop1);
              ^

              C Offline
              C Offline
              Chrisw01
              wrote on 24 Oct 2019, 17:59 last edited by
              #6

              @AnneRanch My bad, try BT_string_list.at(loop1).name();

              1 Reply Last reply
              1
              • J JonB
                24 Oct 2019, 17:59

                @AnneRanch
                The object you have there is a whole QBluetoothHostInfo. The error message tells you the << operator hasn't been written to cope with that. You can't convert a whole QBluetoothHostInfo to a QString. @Pablo-J-Rogina is suggesting you output a QBluetoothHostInfo::name(), just the name of it. So presumably

                cout << BT_string_list.at(loop1).name();
                
                A Offline
                A Offline
                Anonymous_Banned275
                wrote on 24 Oct 2019, 23:54 last edited by
                #7

                @JonB

                Sorry, still no good.

                Here is the code

                  cout << BT_string_list.at(loop1).name(); 
                

                won't even compile

                  qDebug ("DEBUG  message %d, says: %s",
                          BT_string_list.size(),BT_string_list.at(loop1).name());
                

                runs but "prints" garbage

                And error
                /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:134: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'QString')
                cout << BT_string_list.at(loop1).name();
                ^

                Same issue
                the BT_string_list.at(loop returns "item" ( per doc ) and I have no clue what is "item" .

                The .name() , assuming it is a one of the items should return QString .
                Neither qDebug or "loop ..." see a QString - that is the issue.

                In simple terms - I need to convert whatever type ins "item" to QString to be able to send it to console. I am open to options, but woudl like to stick with C++ std::cout . This QString intead of C++ "String" is making things unnecessary complicated to code in Qt.

                P J.HilkJ J 3 Replies Last reply 25 Oct 2019, 00:15
                0
                • A Anonymous_Banned275
                  24 Oct 2019, 23:54

                  @JonB

                  Sorry, still no good.

                  Here is the code

                    cout << BT_string_list.at(loop1).name(); 
                  

                  won't even compile

                    qDebug ("DEBUG  message %d, says: %s",
                            BT_string_list.size(),BT_string_list.at(loop1).name());
                  

                  runs but "prints" garbage

                  And error
                  /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:134: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'QString')
                  cout << BT_string_list.at(loop1).name();
                  ^

                  Same issue
                  the BT_string_list.at(loop returns "item" ( per doc ) and I have no clue what is "item" .

                  The .name() , assuming it is a one of the items should return QString .
                  Neither qDebug or "loop ..." see a QString - that is the issue.

                  In simple terms - I need to convert whatever type ins "item" to QString to be able to send it to console. I am open to options, but woudl like to stick with C++ std::cout . This QString intead of C++ "String" is making things unnecessary complicated to code in Qt.

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 25 Oct 2019, 00:15 last edited by
                  #8

                  @AnneRanch said in How to output QList to standard console ?:

                  the BT_string_list.at(loop returns "item" ( per doc ) and I have no clue what is "item" .

                  Wow, it's something you defined yourself...

                  QList<QBluetoothHostInfo> BT_string_list;

                  so "item" will be an object of class ___________ (please fill in the blanks)

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • A Anonymous_Banned275
                    24 Oct 2019, 23:54

                    @JonB

                    Sorry, still no good.

                    Here is the code

                      cout << BT_string_list.at(loop1).name(); 
                    

                    won't even compile

                      qDebug ("DEBUG  message %d, says: %s",
                              BT_string_list.size(),BT_string_list.at(loop1).name());
                    

                    runs but "prints" garbage

                    And error
                    /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:134: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'QString')
                    cout << BT_string_list.at(loop1).name();
                    ^

                    Same issue
                    the BT_string_list.at(loop returns "item" ( per doc ) and I have no clue what is "item" .

                    The .name() , assuming it is a one of the items should return QString .
                    Neither qDebug or "loop ..." see a QString - that is the issue.

                    In simple terms - I need to convert whatever type ins "item" to QString to be able to send it to console. I am open to options, but woudl like to stick with C++ std::cout . This QString intead of C++ "String" is making things unnecessary complicated to code in Qt.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 25 Oct 2019, 08:25 last edited by J.Hilk
                    #9

                    @AnneRanch
                    std::cout knows nothing of QString, by default at least, but it can print std::strings

                    therefore:

                    cout << BT_string_list.at(loop1).name().toStdString(); 
                    

                    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.

                    A 1 Reply Last reply 26 Oct 2019, 04:17
                    5
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on 25 Oct 2019, 08:34 last edited by
                      #10

                      https://doc.qt.io/qt-5/qtglobal.html#qPrintable

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      4
                      • A Anonymous_Banned275
                        24 Oct 2019, 23:54

                        @JonB

                        Sorry, still no good.

                        Here is the code

                          cout << BT_string_list.at(loop1).name(); 
                        

                        won't even compile

                          qDebug ("DEBUG  message %d, says: %s",
                                  BT_string_list.size(),BT_string_list.at(loop1).name());
                        

                        runs but "prints" garbage

                        And error
                        /media/z/DOC_COPY_LABEL/Projects /QT_TEST/TEST_Splitter/application_Form_1/application/mainwindow_form.cpp:134: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'QString')
                        cout << BT_string_list.at(loop1).name();
                        ^

                        Same issue
                        the BT_string_list.at(loop returns "item" ( per doc ) and I have no clue what is "item" .

                        The .name() , assuming it is a one of the items should return QString .
                        Neither qDebug or "loop ..." see a QString - that is the issue.

                        In simple terms - I need to convert whatever type ins "item" to QString to be able to send it to console. I am open to options, but woudl like to stick with C++ std::cout . This QString intead of C++ "String" is making things unnecessary complicated to code in Qt.

                        J Offline
                        J Offline
                        JonB
                        wrote on 25 Oct 2019, 09:05 last edited by JonB
                        #11

                        @AnneRanch said in How to output QList to standard console ?:

                        qDebug ("DEBUG message %d, says: %s", BT_string_list.size(),BT_string_list.at(loop1).name());

                        runs but "prints" garbage

                        That's because you have not looked up the syntax or examples of qDebug(). Always use the (excellent) documentation! Look at https://doc.qt.io/qt-5/qdebug.html. See how it always uses the << operator? So

                        qDebug() << "DEBUG  message " << BT_string_list.size() << ", says: " << BT_string_list.at(loop1).name();
                        
                        1 Reply Last reply
                        3
                        • J.HilkJ J.Hilk
                          25 Oct 2019, 08:25

                          @AnneRanch
                          std::cout knows nothing of QString, by default at least, but it can print std::strings

                          therefore:

                          cout << BT_string_list.at(loop1).name().toStdString(); 
                          
                          A Offline
                          A Offline
                          Anonymous_Banned275
                          wrote on 26 Oct 2019, 04:17 last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0

                          1/12

                          24 Oct 2019, 16:08

                          • Login

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