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

how to display Ports Available List



  • hello
    i want to display Ports available on my computer using QSerialPortInfo::availablePorts()
    using the following code

    #include <QCoreApplication>
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QList>
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QSerialPortInfo s1;
        QList<QSerialPortInfo> list;
        list=s1.availablePorts();
    qDebug()<<list;
    
        return a.exec();
    }
    

    but i receive error on

    qDebug()<<list;
    

    how to convert "list" to Qstring in order to use with qDebug()?



  • @rezaMSLM
    I assume from http://doc.qt.io/qt-5/qdebug.html#operator-lt-lt-25

    Writes the contents of list to debug. T needs to support streaming into QDebug.

    it's not the QList that is the issue, it's that QSerialPortInfo cannot be streamed (turned into a string). You need to do that yourself, perhaps one-at-a-time instead of in one go.


  • Moderators

    @rezaMSLM

    one way of doing it.

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        
        for(const QSerialPortInfo &info : QSerialPortInfo::availablePorts())
            qDebug() << info.portName();
    
        return a.exec();
    }
    

    Fixed typo.



  • @J.Hilk
    I'm a newbie please post complete code. this code has compiling errors.


  • Moderators

    @rezaMSLM well yes, I missed a closing ), you should be able to fix that yourself.

    for(const QSerialPortInfo &info : QSerialPortInfo::availablePorts())
            qDebug() << info.portName();
    

Log in to reply