How QT uses QProcess to obtain computer hardware information

1. Project introduction

This article introduces the use of QProcess to obtain computer-related hardware information such as the CPU, motherboard, and hard disk of the computer.

Windows provides "wmic" (Windows Management Instrumentation, Windows Management Tools), which provides support for performing system management from the command line interface and batch command scripts. You can open cmd and enter the following commands in it to obtain relevant information.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

//获取cpu名称
wmic cpu get Name
//获取cpu核心数
wmic cpu get NumberOfCores
//获取cpu线程数
wmic cpu get NumberOfLogicalProcessors
//查询cpu:
wmic cpu get processorid
//查询主板:
wmic baseboard get serialnumber
//查询BIOS:
wmic bios get serialnumber
//查看硬盘:
wmic diskdrive get serialnumber
//获取主板序唯一标识:
wmic csproduct get uuid
//查询网卡连接唯一标识:
Wmic Path Win32_NetworkAdapter get GUID
//查询网卡物理地址:
Wmic Path Win32_NetworkAdapter get NetEnabled
//查询网卡是否启用:
Wmic Path Win32_NetworkAdapter get NetEnabled
//查询网卡是否为物理适配器:
Wmic Path Win32_NetworkAdapter get PhysicalAdapter
//查询网卡索引号:
Wmic Path Win32_NetworkAdapter get Index

2. Basic project configuration

Create a new Qt case, the project name is "InfoTest", select "QWidget" as the base class, uncheck the Create UI interface check box, and complete the project creation.

【Notes】: This program is only available for Qt5, and Qt6 does not support it. About how to run in Qt6, you can directly see the last part of this article

3. UI interface settings

No UI interface

4. Realization of the main program

4.1 widget.h header file

First define a private function in the header file:

private:
    QString getInfo(const QString &cmd);

and 8 public functions:

public:
    QString getCpuName();
    QString getCpuCore();
    QString getCpuProcessors();
    QString getCpuProcessorid();
    QString getBaseboardSerialnumber();
    QString getBiosSerialnumber();
    QString getBaseboardUuid();
    QString getDiskSerialnumber();

4.2 widget.cpp source file

Define the getInfo function:

QString Widget::getInfo(const QString &cmd)
{
    QProcess p;        //启动外部程序
    p.start(cmd);      //一体式启动,不分离,主程序退出则启动程序退出,使用close关闭
    //p.startDetached(cmd)  //分离式启动,主程序退出后,外部程序继续运行
    p.waitForFinished(-1);  //超时等待,设置为-1,直到执行完成
    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());//读取运行结果
    QStringList list = cmd.split(" ");  //将cmd按空格拆分
    result = result.remove(list.last(), Qt::CaseInsensitive);//删除cmd序列中最后一个元素
    result = result.replace("\r", "");  //删除\r
    result = result.replace("\n", "");  //删除\n
    result = result.simplified();       //移除字符串两端的空白字符
    p.close();
    return result;
}

Then define seven functions to obtain computer-related hardware information, such as CPU, number of CPU cores, number of CPU threads, etc. The code is as follows:

//查询CPU型号
QString Widget::getCpuName()
{
  return getInfo("wmic cpu get Name");
}

//查询CPU核心数
QString Widget::getCpuCore()
{
    return getInfo("wmic cpu get NumberOfCores");
}
//查询CPU线程数
QString Widget::getCpuProcessors()
{
    return getInfo("wmic cpu get NumberOfLogicalProcessors");
}
//查询CPU处理器标识符
QString Widget::getCpuProcessorid()
{
     return getInfo("wmic cpu get processorid");
}
//查询主板序列号
QString Widget::getBaseboardSerialnumber()
{
     return getInfo("wmic baseboard get serialnumber");
}
//查询BIOS序列号
QString Widget::getBiosSerialnumber()
{
     return getInfo("wmic bios get serialnumber");
}
//查询主板唯一标识符
QString Widget::getBaseboardUuid()
{
     return getInfo("wmic csproduct get uuid");
}
//查询硬盘标识符
QString Widget::getDiskSerialnumber()
{
     return getInfo("wmic diskdrive get serialnumber");
}

Finally, reference the above function in the constructor:

First construct the interface layout: (You can also create a new layout directly in the ui)

    //新建布局
    QLabel *cpuLabel=new QLabel("CPU:");
    QLineEdit *cpuLineEdit=new QLineEdit;
    QLabel *coreLabel=new QLabel("CPU核心数:");
    QLineEdit *coreLineEdit=new QLineEdit;
    QLabel *threadLabel=new QLabel("CPU线程数:");
    QLineEdit *threadLineEdit=new QLineEdit;
    QLabel *idLabel=new QLabel("CPU处理器标识符:");
    QLineEdit *idLineEdit=new QLineEdit;
    QLabel *baseboardLabel=new QLabel("主板序列号:");
    QLineEdit *baseboardLineEdit=new QLineEdit;
    QLabel *biosLabel=new QLabel("Bios序列号:");
    QLineEdit *biosLineEdit=new QLineEdit;
    QLabel *BidLabel=new QLabel("主板标识符:");
    QLineEdit *BidLineEdit=new QLineEdit;
    QLabel *diskLabel=new QLabel("硬盘序列号:");
    QLineEdit *diskLineEdit=new QLineEdit;
    //布局
    QGridLayout *Layout=new QGridLayout(this);
    Layout->addWidget(cpuLabel,0,0);
    Layout->addWidget(cpuLineEdit,0,1);
    Layout->addWidget(coreLabel,1,0);
    Layout->addWidget(coreLineEdit,1,1);
    Layout->addWidget(threadLabel,2,0);
    Layout->addWidget(threadLineEdit,2,1);
    Layout->addWidget(idLabel,3,0);
    Layout->addWidget(idLineEdit,3,1);
    Layout->addWidget(baseboardLabel,4,0);
    Layout->addWidget(baseboardLineEdit,4,1);
    Layout->addWidget(biosLabel,5,0);
    Layout->addWidget(biosLineEdit,5,1);
    Layout->addWidget(BidLabel,6,0);
    Layout->addWidget(BidLineEdit,6,1);
    Layout->addWidget(diskLabel,7,0);
    Layout->addWidget(diskLineEdit,7,1);

The interface after construction is as follows:

 

Then reference the function to get hardware information:

    //获取硬件信息
    cpuLineEdit->setText(getCpuName());
    coreLineEdit->setText(getCpuCore());
    threadLineEdit->setText(getCpuProcessors());
    idLineEdit->setText(getCpuProcessorid());
    baseboardLineEdit->setText(getBaseboardSerialnumber());
    biosLineEdit->setText(getBiosSerialnumber());
    BidLineEdit->setText(getBaseboardUuid());
    diskLineEdit->setText(getDiskSerialnumber());

5. Effect demonstration

The complete effect is as follows:

 

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130657889