Qt官方示例-枚舉器

❝該示例演示如何顯示系統中串列設備的資訊。 ❞

  此GUI示例在類QSerialPortInfo中提供有關小部件中的串列埠的資訊。要獲取有關可用埠的資訊,請使用靜態方法availablePorts()。

  示例源碼:

#include <QLabel>  #include <QScrollArea>  #include <QSerialPortInfo>  #include <QVBoxLayout>  #include <QWidget>    int main(int argc, char *argv[])  {      QApplication a(argc, argv);        auto layout = new QVBoxLayout;        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";            auto label = new QLabel(s);          layout->addWidget(label);      }        auto workPage = new QWidget;      workPage->setLayout(layout);        QScrollArea area;      area.setWindowTitle(QObject::tr("Info about all available serial ports."));      area.setWidget(workPage);      area.show();        return a.exec();  }  

關於更多

  • 在「QtCreator軟體」可以找到:
  • 或在以下「Qt安裝目錄」找到:
C:Qt{你的Qt版本}Examples{你的Qt版本}serialportenumerator  
  • 「相關鏈接」
https://doc.qt.io/qt-5/qtserialport-enumerator-example.html  

Tags: