項目實戰:Qt監測操作系統cpu溫度v1.1.0(支持windows、linux、國產麒麟系統)

需求

  使用Qt軟件開發一個檢測cpu溫度的功能。
  兼容windows、linux,國產麒麟系統(同爲linux)

Demo

  windows上運行(需要管理員權限):
  在這裏插入圖片描述

  國產麒麟操作上運行(需要管理員權限):
  在這裏插入圖片描述

 

功能描述 v1.1.0

  • windows上定時檢測輸出cpu溫度。
  • linux上定時檢測輸出cpu溫度。
  • 國產銀河麒麟操作系統上輸出cpu溫度。
 

模塊化部署

  在這裏插入圖片描述

 

關鍵源碼

#ifndef LINUX
    QString cmd = QString("wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature");

    QProcess process;
    process.start(cmd);
    process.waitForFinished();

    QString result = process.readAllStandardOutput();
    LOG << result;

    result = result.replace("\r", "");
    LOG << result;
   
    QStringList list = result.split("\n", QString::SkipEmptyParts);
    LOG << list;

    bool ok = false;
    int t = 0;
    for(int index = 0; index < list.size(); index++)
    {
        QString str = list.at(index);
        str = str.trimmed();
        LOG << str;
        t = str.toInt(&ok);
        if(ok)
        {
            break;
        }
    }
    // false失敗
    if(!ok)
    {
        emit signal_detectTemperature(false, _t);
        // 下一次檢測
        QTimer::singleShot(_intervalMs, this, SLOT(slot_loop()));
        return;
    }

    // 轉換
    _t = (t - 2732) * 1.0f / 10;

    // 拋出溫度
    emit signal_detectTemperature(true, _t);
    
#else

    // sensors,有些電腦可能沒安裝,安裝方法如下:
    // sudo apt-get install lm-sensors hddtemp
    // sudo sensors-detect
    // sensors

    QProcess process;
    process.start("sensors");
    process.waitForFinished();

    QString result = process.readAllStandardOutput();
    LOG << result;

    result = result.replace("\r","");
    LOG << result;

    bool ok = false;
    QStringList list = result.split("\n", QString::SkipEmptyParts);
    LOG << list;
#if 1
    for(int index = 0; index < list.size(); index++)
    {
        QString str = list.at(index);
        // 注意:
        //      1.虛擬機是無法獲取溫度的
        // Physical id 0: +39.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 0:        +33.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 1:        +35.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 2:        +36.0°C  (high = +80.0°C, crit = +100.0°C)
        // Core 3:        +39.0°C  (high = +80.0°C, crit = +100.0°C)
        if(str.contains(
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章