用QT作为开发工具使用OpenHardwareMonitorLib获取CPU温度代码
以下是使用QT和OpenHardwareMonitorLib获取CPU温度的示例代码:
#include <QCoreApplication>
#include <iostream>
#include "OpenHardwareMonitorLib.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Initialize OpenHardwareMonitorLib
OpenHardwareMonitorLib::Computer computer;
computer.Open();
// Loop through all hardware components
for (int i = 0; i < computer.HardwareCount(); ++i)
{
// Get hardware component
OpenHardwareMonitorLib::Hardware hardware = computer.Hardware(i);
hardware.Update();
// Check if hardware is CPU
if (hardware.HardwareType() == OpenHardwareMonitorLib::HardwareType::CPU)
{
// Loop through all sensors of the CPU
for (int j = 0; j < hardware.SensorCount(); ++j)
{
OpenHardwareMonitorLib::Sensor sensor = hardware.Sensor(j);
// Check if sensor is temperature sensor
if (sensor.SensorType() == OpenHardwareMonitorLib::SensorType::Temperature)
{
// Get CPU temperature
std::cout << "CPU Temperature: " << sensor.Value() << " " << sensor.Unit() << std::endl;
}
}
}
}
// Close OpenHardwareMonitorLib
computer.Close();
return a.exec();
}
请确保你已经正确安装并链接了OpenHardwareMonitorLib库
原文地址: https://www.cveoy.top/t/topic/hFjk 著作权归作者所有。请勿转载和采集!