Qt 获取Windows内存信息

Posted GreenArrowMan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt 获取Windows内存信息相关的知识,希望对你有一定的参考价值。

通过调用Windows API来获取Windows 内存信息,并显示在界面上,通过与Windows 内存管理器对比可以看到结果是正确的。

实现的小工具的界面效果与内存管理器对比如下:


实现的完整代码:

#pragma execution_character_set("utf-8")
#ifndef QMEMORYINFO_H
#define QMEMORYINFO_H

#include <QtWidgets/QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QTimer>

class QMemoryInfo : public QWidget
{
	Q_OBJECT

public:
	QMemoryInfo(QWidget *parent = 0);
	~QMemoryInfo();

private:
	void updateInfo();

	QTimer *m_timer;

	QLabel *m_memoryUsedLabel;
	QLabel *m_totalPhysLabel;
	QLabel *m_availPhysLabel;
	QLabel *m_totalPageFileLabel;
	QLabel *m_availPageFileLabel;
	QLabel *m_totalVirtualLabel;
	QLabel *m_availVirtualLabel;
	QLabel *m_availExVirtualLabel;

	QLineEdit *m_memoryUsedEdit;
	QLineEdit *m_totalPhysEdit;
	QLineEdit *m_availPhysEdit;
	QLineEdit *m_totalPageFileEdit;
	QLineEdit *m_availPageFileEdit;
	QLineEdit *m_totalVirtualEdit;
	QLineEdit *m_availVirtualEdit;
	QLineEdit *m_availExVirtualEdit;
};

#endif // QMEMORYINFO_H
#include "qmemoryinfo.h"
#include <QGridLayout>
#include <windows.h>
#include <sysinfoapi.h>

QMemoryInfo::QMemoryInfo(QWidget *parent)
	: QWidget(parent)
{
	m_memoryUsedLabel = new QLabel(this);
	m_totalPhysLabel = new QLabel(this);
	m_availPhysLabel = new QLabel(this);
	m_totalPageFileLabel = new QLabel(this);
	m_availPageFileLabel = new QLabel(this);
	m_totalVirtualLabel = new QLabel(this);
	m_availVirtualLabel = new QLabel(this);
	m_availExVirtualLabel = new QLabel(this);

	m_memoryUsedEdit = new QLineEdit(this);
	m_totalPhysEdit = new QLineEdit(this);
	m_availPhysEdit = new QLineEdit(this);
	m_totalPageFileEdit = new QLineEdit(this);
	m_availPageFileEdit = new QLineEdit(this);
	m_totalVirtualEdit = new QLineEdit(this);
	m_availVirtualEdit = new QLineEdit(this);
	m_availExVirtualEdit = new QLineEdit(this);

	m_memoryUsedLabel->setText(QObject::tr("内存使用率:"));
	m_totalPhysLabel->setText("总物理内存:");
	m_availPhysLabel->setText("可用物理内存:");
	m_totalPageFileLabel->setText("系统提交总数:");
	m_availPageFileLabel->setText("系统提交数:");
	m_totalVirtualLabel->setText("总虚拟内存:");
	m_availVirtualLabel->setText("可用虚拟内存:");
	m_availExVirtualLabel->setText("可用扩展虚拟:");

	m_memoryUsedEdit->setReadOnly(true);
	m_totalPhysEdit->setReadOnly(true);
	m_availPhysEdit->setReadOnly(true);
	m_totalPageFileEdit->setReadOnly(true);
	m_availPageFileEdit->setReadOnly(true);
	m_totalVirtualEdit->setReadOnly(true);
	m_availVirtualEdit->setReadOnly(true);
	m_availExVirtualEdit->setReadOnly(true);

	QGridLayout *pLay = new QGridLayout(this);
	int iLine = 0;
	pLay->addWidget(m_memoryUsedLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_memoryUsedEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_totalPhysLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_totalPhysEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_availPhysLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_availPhysEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_totalPageFileLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_totalPageFileEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_availPageFileLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_availPageFileEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_totalVirtualLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_totalVirtualEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_availVirtualLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_availVirtualEdit, iLine, 1, 1, 3);
	iLine++;
	pLay->addWidget(m_availExVirtualLabel, iLine, 0, 1, 1);
	pLay->addWidget(m_availExVirtualEdit, iLine, 1, 1, 3);

	m_timer = new QTimer(this);
	connect(m_timer, &QTimer::timeout, this, &QMemoryInfo::updateInfo);
	m_timer->start(100);
}

QMemoryInfo::~QMemoryInfo()
{

}

void QMemoryInfo::updateInfo()
{
	MEMORYSTATUSEX memoryInfo;
	memoryInfo.dwLength = sizeof(memoryInfo);
	GlobalMemoryStatusEx(&memoryInfo);

	UINT mb = 1024 * 1024;
	m_memoryUsedEdit->setText(QString::number(memoryInfo.dwMemoryLoad).append("%"));
	m_totalPhysEdit->setText(QString::number(memoryInfo.ullTotalPhys / mb));
	m_availPhysEdit->setText(QString::number(memoryInfo.ullAvailPhys / mb));
	m_totalPageFileEdit->setText(QString::number(memoryInfo.ullTotalPageFile / mb));
	m_availPageFileEdit->setText(QString::number(memoryInfo.ullTotalPageFile / mb - memoryInfo.ullAvailPageFile / mb));
	m_totalVirtualEdit->setText(QString::number(memoryInfo.ullTotalVirtual / mb));
	m_availVirtualEdit->setText(QString::number(memoryInfo.ullAvailVirtual / mb));
	m_availExVirtualEdit->setText(QString::number(memoryInfo.ullAvailExtendedVirtual / mb));
	update();
}
交流qq:1245178753

本文地址:http://blog.csdn.net/u011417605/article/details/51564802

源码下载:http://download.csdn.net/detail/u011417605/9538726

以上是关于Qt 获取Windows内存信息的主要内容,如果未能解决你的问题,请参考以下文章

PHP检测获取内存信息

QT 实用代码片段

QT调试时怎么查看某个指针指向的一片内存区域的信息

Qt 扫描进程列表以及获取进程信息

如何使用模块化代码片段中的LeakCanary检测内存泄漏?

Qt扫描进程列表以及获取进程信息