使用QT作为开发工具不使用WMI获取CPU温度、CPU使用率、CPU风扇转速、CPU功耗
要获取CPU温度、CPU使用率、CPU风扇转速和CPU功耗,可以使用Qt的跨平台库来实现。
首先,你需要获取系统的相关信息。在Qt中,可以使用QSysInfo类来获取系统信息,包括CPU信息。然后,你可以使用QProcess类来执行命令行指令,以获取CPU温度、使用率、风扇转速和功耗。
以下是一个示例代码,展示了如何使用Qt获取CPU温度、使用率、风扇转速和功耗:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 获取CPU温度
QProcess process;
process.start("sensors"); // 使用sensors命令获取温度信息
process.waitForFinished();
QString output = process.readAllStandardOutput();
qDebug() << "CPU温度:" << output;
// 获取CPU使用率
process.start("top -bn1 | grep \"Cpu(s)\""); // 使用top命令获取CPU使用率信息
process.waitForFinished();
output = process.readAllStandardOutput();
qDebug() << "CPU使用率:" << output;
// 获取CPU风扇转速
process.start("sensors"); // 使用sensors命令获取风扇转速信息
process.waitForFinished();
output = process.readAllStandardOutput();
qDebug() << "CPU风扇转速:" << output;
// 获取CPU功耗
process.start("cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj"); // 使用cat命令获取CPU功耗信息
process.waitForFinished();
output = process.readAllStandardOutput();
qDebug() << "CPU功耗:" << output;
return a.exec();
}
请注意,以上代码仅适用于Linux系统。如果你使用的是其他操作系统,你需要相应地修改命令行指令以适应你的系统。
原文地址: https://www.cveoy.top/t/topic/hFkn 著作权归作者所有。请勿转载和采集!