CPU, Memory의 사용량을 숫자 또는 ProgressBar를 이용하여 출력하는 예제이다.

 

sysinfoqml.h

#ifndef SYSINFOQML_H
#define SYSINFOQML_H

#include <QObject>
#include "SysInfo.h"

class SysInfoQML : public QObject
{
    Q_OBJECT
    Q_PROPERTY(double cpuTotal READ cpuTotal)
    Q_PROPERTY(double cpuUsage READ cpuUsage)
    Q_PROPERTY(double memTotal READ memTotal)
    Q_PROPERTY(double memUsage READ memUsage)
public:
    SysInfoQML(QObject *parent = 0);

    double cpuTotal() const;
    double cpuUsage() const;

    double memTotal() const;
    double memUsage() const;

public slots:
    void update();

private:
    SysInfo* m_sysInfo;
};

#endif // SYSINFOQML_H

 

sysinfoqml.cpp

#include "SysInfoQML.h"

#ifdef Q_OS_WIN
    #include "WindowsInfo.h"
#elif defined(Q_OS_ANDROID)
    #include "AndroidInfo.h"
#endif


SysInfoQML::SysInfoQML(QObject *parent) : QObject(parent)
{
#ifdef Q_OS_WIN
    this->m_sysInfo = new WindowsInfo();
#elif defined(Q_OS_ANDROID)
    this->m_sysInfo = new AndroidInfo();
#endif

}

double SysInfoQML::cpuTotal() const
{
    return this->m_sysInfo->m_cpuInfo.getTotal();
}

double SysInfoQML::cpuUsage() const
{
    return this->m_sysInfo->m_cpuInfo.getUsage();
}

double SysInfoQML::memTotal() const
{
    return this->m_sysInfo->m_memInfo.getTotal();
}

double SysInfoQML::memUsage() const
{
    return this->m_sysInfo->m_memInfo.getUsage();
}

void SysInfoQML::update()
{
    this->m_sysInfo->update();
}

 

windowsinfo.h

#ifndef WINDOWSINFO_H
#define WINDOWSINFO_H

#include "sysinfo.h"
#include "Windows.h"
#include <QDebug>

class WindowsInfo : public SysInfo
{
private:
    FILETIME idleTime;
    FILETIME kernelTime;
    FILETIME userTime;

    FILETIME last_idleTime;
    FILETIME last_kernelTime;
    FILETIME last_userTime;

public:
    WindowsInfo();

    void update() override;

    qulonglong convertFileTime(const FILETIME& filetime) const;
};

#endif // WINDOWSINFO_H

 

windowsinfo.cpp

#include "windowsinfo.h"
#include <windows.h>

WindowsInfo::WindowsInfo()
{
    //get memory total
    MEMORYSTATUSEX memoryStatus;
    memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memoryStatus);
    this->m_memInfo.setTotal(memoryStatus.ullTotalPhys / 1024 / 1024);

    //get the last systemtime
    GetSystemTimes(&this->last_idleTime, &this->last_kernelTime, &this->last_userTime);
}

void WindowsInfo::update()
{
    //update memory
    MEMORYSTATUSEX memoryStatus;
    memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memoryStatus);
    this->m_memInfo.setUsage((memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys) / 1024 / 1024);

    //update cpu
    GetSystemTimes( &this->idleTime, &this->kernelTime, &this->userTime);
    qulonglong usr, ker, idl, sys;
    usr = convertFileTime(userTime)   - convertFileTime(last_userTime);
    ker = convertFileTime(kernelTime) - convertFileTime(last_kernelTime);
    idl = convertFileTime(idleTime)   - convertFileTime(last_idleTime);
    sys = ker + usr;
    this->m_cpuInfo.setUsage((sys - idl) * 100 / sys);
    last_userTime = userTime;
    last_kernelTime = kernelTime;
    last_idleTime = idleTime;
}

qulonglong WindowsInfo::convertFileTime(const FILETIME& filetime) const
{
    ULARGE_INTEGER largeInteger;
    largeInteger.LowPart = filetime.dwLowDateTime;
    largeInteger.HighPart = filetime.dwHighDateTime;
    return largeInteger.QuadPart;
}

 

Timer를 이용하여 CPU, Memory 사용량을 업데이트 해준다.

 

main.qml

import QtQuick 2.6
import QtQuick.Window 2.2
import SysInfoQML 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property int p_cpuUsage: 0
    property int p_memUsage: 0

    Text{
        x: 220
        y: 10
        text: {
            return "CPU Usage " + p_cpuUsage + "%"
        }
    }

    ProgressBarWidget {
        id:cpuUsageBar
        x: 10
        y: 10
        width: 200
        height: 30
    }

    Text{
        x: 220
        y: 50
        text: {
            return "MEM Usage " + p_memUsage +"%"
        }
    }

    ProgressBarWidget {
        id:memUsageBar
        x: 10
        y: 50
        width: 200
        height: 30
    }

    SysInfoQML {
        id: id_sysInfo
    }

    Timer{
        repeat: true
        interval: 1000
        running: true

        onTriggered: {
            id_sysInfo.update()
            p_cpuUsage = id_sysInfo.cpuUsage
            p_memUsage = id_sysInfo.memUsage * 100 / id_sysInfo.memTotal
            cpuUsageBar.p_usage = id_sysInfo.cpuUsage
            memUsageBar.p_usage = id_sysInfo.memUsage * 100 / id_sysInfo.memTotal
        }
    }
}

 

 

예제)

 

 

 

 

 

아래의 첨부 파일은 위의 소스를 이용하여 만든 예제이다.

첨부파일 : usageResource