[英]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端口信息。 当某些vendorIdentifierand
和productIdentifier
匹配某个数字时,我想在标签中显示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.