码迷,mamicode.com
首页 > Windows程序 > 详细

Qt 获取Windows内存信息

时间:2016-06-02 13:41:14      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:

通过调用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内存信息

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!