簡體   English   中英

清除QSerial正在使用的QList

[英]Clear QList that is being used by QSerial

我有以下代碼:

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 Error”時,我想要它。 這部分不起作用。 上面的代碼由QTimer定位並更新,但是Qlist infos尚未清除。 基本上,我如何清除此Qlist? infos.clear(); 不起作用。

正如@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(","));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM