清除QSerial正在使用的QList

时间:2017-01-08 15:00:25

标签: c++ qt

我有以下代码:

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updatecommstatus()));
timer -> start();

void MainWindow::updatecommstatus()
{
    const auto infos = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &info : infos) {
        QString s = QObject::tr("Port: ") + info.portName() + "\n"
                    + QObject::tr("Location: ") + info.systemLocation() + "\n"
                    + QObject::tr("Description: ") + info.description() + "\n"
                    + QObject::tr("Manufacturer: ") + info.manufacturer() + "\n"
                    + QObject::tr("Serial number: ") + info.serialNumber() + "\n"
                    + QObject::tr("Vendor Identifier: ") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Product Identifier: ") + (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n";
        if (QString::number(info.vendorIdentifier(), 16) == "16d0" && QString::number(info.productIdentifier(), 16) == "650")
        {
            ui->label_commport->setText(info.portName());
        }
        else 
        {
            ui->label_commport->setText("COM Error");
        }
    }
}

利用QSerial显示所有可用的COM端口信息。我想在某个vendorIdentifierandproductIdentifier匹配某个数字时在标签中显示portName。

以上代码在插入设备时效果很好(我的标签显示正确的信息)。但是当我拔掉标签显示COM错误时我想要它。这部分不起作用。上述代码由QTimer定位并更新,但Qlist infos未清除。基本上,我该如何清除这个Qlist? infos.clear();不起作用。

1 个答案:

答案 0 :(得分:1)

正如@lucaAngiolini在他的评论中提到的那样,标签更新的范围似乎是错误的。我认为您尝试将所有可用端口编译为字符串,然后再设置标签。

void MainWindow::updatecommstatus()
{
    const auto infos = QSerialPortInfo::availablePorts();
    QStringList comport_labels;
    if (infos.empty())
        comprt_labels << "COM Error";
    for (const QSerialPortInfo &info : infos) {
        QString s = QObject::tr("Port: ") + info.portName() + "\n"
                    + QObject::tr("Location: ") + info.systemLocation() + "\n"
                    + QObject::tr("Description: ") + info.description() + "\n"
                    + QObject::tr("Manufacturer: ") + info.manufacturer() + "\n"
                    + QObject::tr("Serial number: ") + info.serialNumber() + "\n"
                    + QObject::tr("Vendor Identifier: ") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Product Identifier: ") + (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n";
        if (QString::number(info.vendorIdentifier(), 16) == "16d0" && QString::number(info.productIdentifier(), 16) == "650")
        {
            comport_labels << info.portName();
        }
        else 
        {
            comport_labels << "COM Error";
        }
    }

    ui->label_commport->setText(comport_labels.join(","));
}