Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to output QList to standard console ?



  • 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;
    


  • Have you tried something like this?

    for(int loop1 = 0; loop1 < BT_string_list.size(); loop1++) {
        cout << BT_string_list.at(loop1);
    }
    


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



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



  • @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();
    


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



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



  • @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)


  • Moderators

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




  • @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();
    


  • This post is deleted!

Log in to reply