若该文为原创文章,转载请注明出处
本文章博客地址:https://hpzwl.blog.csdn.net/article/details/136277231
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)
Qt开发专栏:项目实战(点击传送门)
需求
使用Qt软件开发一个检测cpu温度的功能。
兼容Windows、linux,国产麒麟系统(同为linux)
Demo
windows上运行(需要管理员权限):
国产麒麟操作上运行(需要管理员权限):
windows运行包下载地址
CSDNf粉丝0积分下载:https://download.csdn.net/download/qq21497936/88874614
QQ群:博客首页扫码进入QQ技术群,点击“文件”搜索“monitorCpuT”,群内与博文同步更新)
功能描述 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 QString str = list.at(index); str = str.trimmed(); LOG 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 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 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 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