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 Updated to NodeBB v4.3 + New Features

How to output QList to standard console ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 5.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.
  • C Offline
    C Offline
    Chrisw01
    wrote on 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
    2
    • A Anonymous_Banned275

      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;
      
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on 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

        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 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);
        ^

        JonBJ C 2 Replies Last reply
        0
        • A Anonymous_Banned275

          @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);
          ^

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          3
          • A Anonymous_Banned275

            @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 last edited by
            #6

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

            1 Reply Last reply
            1
            • JonBJ JonB

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

              Pablo J. RoginaP J.HilkJ JonBJ 3 Replies Last reply
              0
              • A Anonymous_Banned275

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

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on 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

                  @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 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
                  5
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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

                        @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 last edited by
                        #12
                        This post is deleted!
                        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