Solved 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-25Writes the contents of list to debug. T needs to support streaming into QDebug.
it's not the
QList
that is the issue, it's thatQSerialPortInfo
cannot be streamed (turned into a string). You need to do that yourself, perhaps one-at-a-time instead of in one go. -
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. -
@rezaMSLM well yes, I missed a closing
)
, you should be able to fix that yourself.for(const QSerialPortInfo &info : QSerialPortInfo::availablePorts()) qDebug() << info.portName();