Windows下Qt读取系统的内存、CPU、GPU等使用信息

一、前言

在当今计算机应用广泛的领域中,了解系统的内存、CPU和GPU使用情况是非常重要的。对于开发人员和系统管理员来说,准确获取这些信息可以帮助他们优化软件性能、诊断问题并做出相应的调整。在Windows平台上实现这一目标会涉及到调用Windows系统API,使用合适的工具和库来获取所需的信息。

本文将介绍如何使用Qt和Windows API来读取系统的内存、CPU和GPU使用详细信息。将提供一个完整的示例代码,展示了如何使用这些技术来获取系统的关键性能指标。通过阅读本文,将学习如何使用Qt框架和Windows API来实现这些功能,以及如何根据需求进行扩展和定制。

二、获取系统的配置信息

cpp 复制代码
​
#include <QApplication>
​
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QLabel>
#include <QSysInfo>
#include <QProcess>
#include <QDebug>
​
#include <QDebug>
#include <Windows.h>
​
​
#pragma execution_character_set("utf-8")
​
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
​
​
    QMainWindow window;
     window.resize(400, 300);
​
     QLabel *label = new QLabel(&window);
     label->setAlignment(Qt::AlignCenter);
     label->setWordWrap(true);
     window.setCentralWidget(label);
​
     // 获取系统内存信息
     QString memoryInfo = "Memory Information:\n";
​
     MEMORYSTATUSEX memoryStatus;
         memoryStatus.dwLength = sizeof(memoryStatus);
         if (GlobalMemoryStatusEx(&memoryStatus)) {
             memoryInfo+=QString("Total Physical Memory: %1 %2\n").arg(memoryStatus.ullTotalPhys / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Available Physical Memory: %1 %2\n").arg(memoryStatus.ullAvailPhys / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Total Virtual Memory: %1 %2\n").arg(memoryStatus.ullTotalVirtual / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Available Virtual Memory: %1 %2\n").arg(memoryStatus.ullAvailVirtual / (1024 * 1024)).arg("MB");
         } else {
             memoryInfo+=QString("无法获取内存使用情况信息。\n");
         }
​
     // 获取CPU信息
     QString cpuInfo = "CPU Information:\n";
     QProcess cpuProcess;
     cpuProcess.start("wmic cpu get Name");
     cpuProcess.waitForFinished();
     QString cpuResult = cpuProcess.readAllStandardOutput();
     QString cpuName = cpuResult.split("\n").at(1).trimmed();
     cpuInfo += "Model: " + cpuName + "\n";
​
     // 获取GPU信息
     QString gpuInfo = "GPU Information:\n";
     QProcess gpuProcess;
     gpuProcess.start("wmic path win32_VideoController get Name");
     gpuProcess.waitForFinished();
     QString gpuResult = gpuProcess.readAllStandardOutput();
     QStringList gpuList = gpuResult.split("\n", QString::SkipEmptyParts);
     for (int i = 1; i < gpuList.size(); i++) {
         QString gpuName = gpuList.at(i).trimmed();
         gpuInfo += "GPU " + QString::number(i) + ": " + gpuName + "\n";
     }
​
     // 在标签中显示系统信息
     QString systemInfo = memoryInfo + "\n" + cpuInfo + "\n" + gpuInfo;
     label->setText(systemInfo);
​
     window.show();
​
​
    //Widget w;
    //w.show();
    return a.exec();
}

三、wmic

wmic是Windows Management Instrumentation Command-line(WMI命令行)实用工具的缩写。它提供了一个命令行界面,可以通过WMI接口与操作系统进行交互和管理。以下是对wmic的详细介绍:

【1】基本概念:Windows Management Instrumentation(WMI)是微软提供的一种标准化的系统管理技术,允许开发人员和管理员使用编程方式来监视和控制Windows操作系统上的资源。WMI提供了一个信息框架,以获取有关计算机硬件、软件和操作系统配置的详细信息。

【2】功能:wmic允许用户通过命令行执行各种系统管理任务,包括查询、修改和监视操作系统中的各种设置和资源,如进程、服务、磁盘驱动器、网络适配器等。它还可以与远程计算机通信,并将结果输出为文本、XML或HTML格式。通过wmic,你可以轻松地获取系统信息、执行管理任务和编写自动化脚本。

【3】语法和用法:wmic的基本语法是wmic <命令> [参数]

常用的命令包括:

  • wmic os:获取操作系统的详细信息。
  • wmic cpu:获取CPU的信息。
  • wmic process:获取正在运行的进程列表。
  • wmic service:获取系统服务的信息。
  • wmic logicaldisk:获取逻辑磁盘驱动器的信息。
  • wmic nicconfig:获取网络适配器配置的信息。

示例用法:以下是使用wmic命令获取操作系统信息和CPU信息的示例:

  • wmic os get Caption, Version, OSArchitecture:获取操作系统的名称、版本和体系结构。
  • wmic cpu get Name, MaxClockSpeed, Manufacturer:获取CPU的名称、最大时钟速度和制造商。

对于更复杂的查询和操作,可以使用WQL(WMI查询语言)来结合wmic命令。WQL类似于SQL,可以用于过滤和排序数据,并执行高级的系统管理任务。

相关推荐
神奇的程序员3 小时前
从已损坏的备份中拯救数据
运维·后端·前端工程化
oden4 小时前
AI服务商切换太麻烦?一个AI Gateway搞定监控、缓存和故障转移(成本降40%)
后端·openai·api
李慕婉学姐5 小时前
【开题答辩过程】以《基于Android的出租车运行监测系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·后端·vue
m0_740043735 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
招风的黑耳6 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
Miss_Chenzr6 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
期待のcode6 小时前
Springboot核心构建插件
java·spring boot·后端
2501_921649496 小时前
如何获取美股实时行情:Python 量化交易指南
开发语言·后端·python·websocket·金融
serendipity_hky7 小时前
【SpringCloud | 第5篇】Seata分布式事务
分布式·后端·spring·spring cloud·seata·openfeign
五阿哥永琪7 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python