Search code examples
c++qtuser-interfaceserial-portqtserialport

Write portname to a label using setText in Qt


I want to try to show portname information in QT. The code is here:

void MainWindow::on_pushButton_clicked()
{
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug() << "Name : " << info.portName();

        // Example use QSerialPort
        QSerialPort serial;
        serial.setPort(info);
        if (serial.open(QIODevice::ReadWrite))
            serial.close();
    }

    ui->label->setText(info.portName());
}

When I compile this code this error comes:

info was not declared in this scope

So, what can I do for showing portname information to label?


Solution

  • I have made a really big mistake. I have realized after sharing this question.

    The true code must be like that:

    void MainWindow::on_pushButton_clicked()
    {
      foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
      {
            qDebug() << "Name : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
    
            // Example use QSerialPort
            QSerialPort serial;
            serial.setPort(info);
            if (serial.open(QIODevice::ReadWrite))
                serial.close();
       ui->label->setText(info.portName());
    }
    

    It means that ui->label->setText(info.portName()); part has to be in parenthesss.