项目实战:Qt监测操作系统cpu温度v1.1.0(支持windows、linux、国产麒麟系统)

需求

  使用Qt软件开发一个检测cpu温度的功能。
  兼容windows、linux,国产麒麟系统(同为linux)

Demo

  windows上运行(需要管理员权限):
  在这里插入图片描述

  国产麒麟操作上运行(需要管理员权限):
  在这里插入图片描述

 

功能描述 v1.1.0

  • windows上定时检测输出cpu温度。
  • linux上定时检测输出cpu温度。
  • 国产银河麒麟操作系统上输出cpu温度。
 

模块化部署

  在这里插入图片描述

 

关键源码

#ifndef LINUX
    QString cmd = QString("wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature");

    QProcess process;
    process.start(cmd);
    process.waitForFinished();

    QString result = process.readAllStandardOutput();
    LOG << result;

    result = result.replace("\r", "");
    LOG << result;
   
    QStringList list = result.split("\n", QString::SkipEmptyParts);
    LOG << list;

    bool ok = false;
    int t = 0;
    for(int index = 0; index < list.size(); index++)
    {
        QString str = list.at(index);
        str = str.trimmed();
        LOG << str;
        t = str.toInt(&ok);
        if(ok)
        {
            break;
        }
    }
    // false失败
    if(!ok)
    {
        emit signal_detectTemperature(false, _t);
        // 下一次检测
        QTimer::singleShot(_intervalMs, this, SLOT(slot_loop()));
        return;
    }

    // 转换
    _t = (t - 2732) * 1.0f / 10;

    // 抛出温度
    emit signal_detectTemperature(true, _t);
    
#else

    // sensors,有些电脑可能没安装,安装方法如下:
    // sudo apt-get install lm-sensors hddtemp
    // sudo sensors-detect
    // sensors

    QProcess process;
    process.start("sensors");
    process.waitForFinished();

    QString result = process.readAllStandardOutput();
    LOG << result;

    result = result.replace("\r","");
    LOG << result;

    bool ok = false;
    QStringList list = result.split("\n", QString::SkipEmptyParts);
    LOG << list;
#if 1
    for(int index = 0; index < list.size(); index++)
    {
        QString str = list.at(index);
        // 注意:
        //      1.虚拟机是无法获取温度的
        // Physical id 0: +39.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 0:        +33.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 1:        +35.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 2:        +36.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 3:        +39.0°C  (high = +80.0°C, crit = +100.0°C)
        if(str.contains("Core 0"))
        {
            LOG << str;
            QStringList subList = str.split(" ", QString::SkipEmptyParts);
            LOG << subList;
            if(subList.size() > 3)
            {
                QString s = subList.at(2);
                // ("Core", "0:", "+33.0°C", "(high", "=", "+80.0°C,", "crit", "=", "+100.0°C)")
                if(s.startsWith("+") && s.endsWith("°C"))
                {
                    _t = s.mid(1, s.size() - 1 - 2).toDouble(&ok);
                }
            }
            break;
        }
    }
#else
    QString str = "Core 0:        +33.2°C  (high = +80.0°C, crit = +100.0°C)";
    if(str.contains("Core 0"))
    {
        LOG << str;
        QStringList subList = str.split(" ", QString::SkipEmptyParts);
        LOG << subList;
        if(subList.size() > 3)
        {
            QString s = subList.at(2);
            // ("Core", "0:", "+33.0°C", "(high", "=", "+80.0°C,", "crit", "=", "+100.0°C)")
            if(s.startsWith("+") && s.endsWith("°C"))
            {
                _t = s.mid(1, s.size() - 1 - 2).toDouble(&ok);
            }
        }
    }
#endif
    // false失败
    if(!ok)
    {
        emit signal_detectTemperature(false, _t);
        // 下一次检测
        QTimer::singleShot(_intervalMs, this, SLOT(slot_loop()));
        return;
    }

    // 抛出温度
    emit signal_detectTemperature(true, _t);

#endif
#endif
 

入坑

入坑一:windows获取不到输出

问题

  在这里插入图片描述

原因

  win10获取系统设备相关信息,需要管理员权限。

解决

  发布时:1.点击exe右键,使用管理员运行。
  发布时/开发时:1.可以直接exe鼠标右键属性,勾选管理员权限运行,后面开发就可以了。
  开发时:2.开发的时候,可以直接使用管理员权限打开QtCreator即可。

入坑二:linux获取不到温度

问题

  没有传感器输出:
  在这里插入图片描述

原因

  因为是使用虚拟机上开发,实际是没有传感器等一些数据的,都是No。(注意:直接开发ubuntu驱动时,查看操作系统上设备树,所有寄存器地址都是0x0000000,也是一样的道理,与硬件相关的,虚拟机是模拟的,没有实际)

解决

  重新装了一台物理麒麟机,测试可以。
  在这里插入图片描述

posted @ 2024-02-26 10:57  长沙红胖子Qt  阅读(131)  评论(0编辑  收藏  举报