• 052-第三代软件开发-系统监测


    头图

    第三代软件开发-系统监测


    关键字: QtQmlcpu内存、memory`

    项目介绍

    欢迎来到我们的 QML & C++ 项目!这个项目结合了 QML(Qt Meta-Object Language)和 C++ 的强大功能,旨在开发出色的用户界面和高性能的后端逻辑。

    在项目中,我们利用 QML 的声明式语法和可视化设计能力创建出现代化的用户界面。通过直观的编码和可重用的组件,我们能够迅速开发出丰富多样的界面效果和动画效果。同时,我们利用 QML 强大的集成能力,轻松将 C++ 的底层逻辑和数据模型集成到前端界面中。

    在后端方面,我们使用 C++ 编写高性能的算法、数据处理和计算逻辑。C++ 是一种强大的编程语言,能够提供卓越的性能和可扩展性。我们的团队致力于优化代码,减少资源消耗,以确保我们的项目在各种平台和设备上都能够高效运行。

    无论您是对 QML 和 C++ 开发感兴趣,还是需要我们为您构建复杂的用户界面和后端逻辑,我们都随时准备为您提供支持。请随时联系我们,让我们一同打造现代化、高性能的 QML & C++ 项目!

    重要说明☝

    ☀该专栏在第三代软开发更新完将涨价

    系统监测

    这个其实是一个临时起意,不属于原本的圆形设计,就是什么呢,我们的程序在启动后,是会置顶全品显示,则就导致我们想看系统CPU占用率和内存使用率变得空难,也不是不可以,就是得再接一块屏幕出来,所以就加了一个功能。代码如下

    头文件

    #ifndef XXXX_SYSTEMINFOR_H
    #define XXXX_SYSTEMINFOR_H
    
    #include 
    #include 
    class XXXX_SystemInfor : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(double cpuRate READ cpuRate WRITE setCpuRate NOTIFY cpuRateChanged FINAL)                       // CPU使用率
        Q_PROPERTY(double memoryUseed READ memoryUseed WRITE setMemoryUseed NOTIFY memoryUseedChanged FINAL)        // 内存使用
        Q_PROPERTY(double memoryTotal READ memoryTotal WRITE setMemoryTotal NOTIFY memoryTotalChanged FINAL)        // 内存总量
    
    public:
        explicit XXXX_SystemInfor(QObject *parent = nullptr);
    
        ~XXXX_SystemInfor();
    
        static XXXX_SystemInfor *getInstance();
    
        double cpuRate() const;
        void setCpuRate(double newCpuRate);
    
        double memoryUseed() const;
        void setMemoryUseed(double newMemoryUseed);
    
        double memoryTotal() const;
        void setMemoryTotal(double newMemoryTotal);
    
    signals:
    
        void cpuRateChanged();
    
        void memoryUseedChanged();
    
        void memoryTotalChanged();
    
    protected:
    
        void getCpuRate();
    
        void getMemoryRate();
    private:
        QTimer*                             m_getSysInfor                                   = nullptr;
        double                              m_cpuRate                                       = 0;
        double                              m_memoryUseed                                   = 0;
        double                              m_memoryTotal                                   = 0;
    };
    
    #endif // XXXX_SYSTEMINFOR_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    源文件

    
    #include "XXXX_systeminfor.h"
    #include "qdebug.h"
    #include "qprocess.h"
    #include 
    Q_GLOBAL_STATIC(XXXX_SystemInfor,XXXX_SystemInfor)          // 单例宏
    XXXX_SystemInfor::XXXX_SystemInfor(QObject *parent)
        : QObject{parent}
    {
        m_getSysInfor = new QTimer(this);
        m_getSysInfor->setInterval(1000 * 2);
        connect(m_getSysInfor,&QTimer::timeout,this,[=](){
            getCpuRate();
            getMemoryRate();
        });
        m_getSysInfor->start();
    }
    /**
     * @brief XXXX_SystemInfor::~XXXX_SystemInfor
     * 析构函数
     */
    XXXX_SystemInfor::~XXXX_SystemInfor()
    {
        m_getSysInfor->stop();
    }
    /**
     * @brief XXXX_SystemInfor::getInstance
     * @return
     * 获取单例
     */
    XXXX_SystemInfor *XXXX_SystemInfor::getInstance()
    {
        return XXXX_SystemInfor;
    }
    /**
     * @brief XXXX_SystemInfor::cpuRate
     * @return
     * 获取cpu使用率
     * linux下使用mpstat命令获取
     * sudo apt-get install sysstat
     * V 2.0 使用top命令获取
     * V 2.1 使用sar命令获取
     * 最终使用V 1.0版本
     */
    void XXXX_SystemInfor::getCpuRate()
    {
    #ifdef Q_OS_LINUX
    
    //    QProcess process;
    //    /// 使用mpstat命令获取CPU使用率
    //    QString program = "sar";
    //    QStringList arguments;
    //    /// 获取所有CPU核心的使用率
    //    arguments << "-u"<< "1" << "1";
    //    process.start(program, arguments);
    //    /// 等待进程完成
    //    process.waitForFinished();
    
    //    process.readLine();
    //    process.readLine();
    //    process.readLine();
    //    process.readLine();
    //    QString str = process.readLine();
    //    str.replace("\n","");
    //    str.replace(QRegExp("( ){1,}")," ");
    //    auto lst = str.split(" ");
    
    
    
    //    if(lst.size() > 6)
    //    {
    
    //        if(lst.last().toDouble() > 0)
    //            setCpuRate(100.00 - lst.last().toDouble());
    //        //        qDebug() << lst.at(7).toDouble();
    //    }
    
    
    
    
    
    
    //    QProcess process;
    //    /// 使用mpstat命令获取CPU使用率
    //    QString program = "top";
    //    QStringList arguments;
    //    /// 获取所有CPU核心的使用率
    //    arguments << "-b"<< "-n" << "1" << "-i" << "-c";
    //    process.start(program, arguments);
    //    /// 等待进程完成
    //    process.waitForFinished();
    
    //    process.readLine();
    //    process.readLine();
    //    QString str = process.readLine();
    //    str.replace("\n","");
    //    str.replace(QRegExp("( ){1,}")," ");
    //    auto lst = str.split(" ");
    //    if(lst.size() > 10)
    //    {
    //        if(lst.at(7).toDouble() > 0)
    //            setCpuRate(100.00 - lst.at(7).toDouble());
            qDebug() << lst.at(7).toDouble();
    //    }
    
    
    
    
        QProcess process;
        /// 使用mpstat命令获取CPU使用率
        QString program = "/usr/bin/mpstat";
        QStringList arguments;
        /// 获取所有CPU核心的使用率
        arguments << "-P" << "ALL";
        process.start(program, arguments);
        /// 等待进程完成
        process.waitForFinished();
        process.readLine();
        process.readLine();
        process.readLine();
        QString str = process.readLine();
        str.replace("\n","");
        str.replace(QRegExp("( ){1,}")," ");
        auto lst = str.split(" ");
        if(lst.size() > 10)
            setCpuRate(100.0 - lst.last().toDouble());
    
    
    #endif
    }
    
    
    /**
     * @brief XXXX_SystemInfor::getMemoryRate
     * @return
     * 获取内存使用率
     */
    void XXXX_SystemInfor::getMemoryRate()
    {
    
    #ifdef Q_OS_LINUX
        QProcess process;
        /// 使用free完成获取
        process.start("free -m");
        process.waitForFinished();
        process.readLine();
        QString str = process.readLine();
        str.replace("\n","");
            /// 将连续空格替换为单个空格 用于分割
        str.replace(QRegExp("( ){1,}")," ");
        auto lst = str.split(" ");
        if(lst.size() > 6)
        {
            setMemoryTotal(lst[1].toDouble());
            setMemoryUseed(lst[1].toDouble() - lst[6].toDouble());
        }
    #endif
    }
    
    /**
     * @brief XXXX_SystemInfor::setMemoryRate
     * @param newMemoryRate
     * CPU 使用率
     */
    double XXXX_SystemInfor::cpuRate() const
    {
        return m_cpuRate;
    }
    /**
     * @brief XXXX_SystemInfor::setCpuRate
     * @param newCpuRate
     * 设置 CPU 使用率
     */
    void XXXX_SystemInfor::setCpuRate(double newCpuRate)
    {
    
        //    if (qFuzzyCompare(m_cpuRate, newCpuRate))
        //        return;
        m_cpuRate = newCpuRate;
        emit cpuRateChanged();
    }
    
    double XXXX_SystemInfor::memoryUseed() const
    {
        return m_memoryUseed;
    }
    
    void XXXX_SystemInfor::setMemoryUseed(double newMemoryUseed)
    {
        //    if (qFuzzyCompare(m_memoryUseed, newMemoryUseed))
        //        return;
        m_memoryUseed = newMemoryUseed;
        emit memoryUseedChanged();
    }
    
    double XXXX_SystemInfor::memoryTotal() const
    {
        return m_memoryTotal;
    }
    
    void XXXX_SystemInfor::setMemoryTotal(double newMemoryTotal)
    {
        //    if (qFuzzyCompare(m_memoryTotal, newMemoryTotal))
        //        return;
        m_memoryTotal = newMemoryTotal;
        emit memoryTotalChanged();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208

    博客签名2021
  • 相关阅读:
    [篇五章二]_使用 USB 系统安装盘在真机上安装激活 Windows 10 LTSC 2021 中文企业版系统
    算法基础学习|前缀和差分
    数据库系统[备考]
    图像处理与计算机视觉--第五章-图像分割-自适应阈值分割
    TCP/IP协议、HTTP协议
    大数据课程L6——网站流量项目的SparkStreaming
    libtorch c++ 定义全链接网络
    阶段六-Day05-MyBatis3
    【LeetCode-简单题】225. 用队列实现栈
    Web3——开发第一个智能合约
  • 原文地址:https://blog.csdn.net/z609932088/article/details/134431219