Linux下获取系统的磁盘使用情况、内存使用情况使用QT界面进行显示

网友投稿 796 2022-05-28

一、环境介绍

操作系统:  ubuntu 18.04 64位  PC机

QT版本:  5.12

二、运行效果图

三、核心代码

mainwindow.cpp代码:

#include "widget.h"

#include "ui_widget.h"

#include

#include

#include

Linux下获取系统的磁盘使用情况、内存使用情况使用QT界面进行显示

#include

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

QTimer::singleShot(1000, this, SLOT(GetSystemInfo()));

}

void Widget::GetSystemInfo(void)

{

/*1. 获取当前系统磁盘使用情况*/

/*

* 格式: /dev/sda1 49G 38G 9.3G 81% /

*/

QProcess process;

process.start("df -h");

process.waitForFinished();

QByteArray output = process.readAllStandardOutput();

QString str_output = output;

str_output=str_output.mid(str_output.indexOf("/dev/sda1"));

//得到: /dev/sda1 49G 38G 9.3G 81%

str_output=str_output.section('/',0,2);

str_output=str_output.section(' ',1);

//将多个空格换成单个空格

str_output=str_output.replace(QRegExp("[\\s]+"), " ");

QString text;

text="磁盘总容量: "+str_output.section(' ',1,1)+"\n";

text+="已用: "+str_output.section(' ',2,2)+"\n";

text+="可用: "+str_output.section(' ',3,3);

//获取百分比

ui->progressBar_rom->setValue(str_output.section(' ',4,4).section('%',0,0).toInt());

ui->label_ROM->setText(text);

/*2. 获取当前系统内存使用情况*/

struct sysinfo s_info;

if(sysinfo(&s_info)==0)

{

text=tr("总内存: %1 KB\n").arg(s_info.totalram/1024);

text+=tr("未使用内存: %1 KB\n").arg(s_info.freeram/1024);

text+=tr("交换区总内存: %1 KB\n").arg(s_info.totalswap/1024);

text+=tr("交换区未使用内存: %1 KB\n").arg(s_info.freeswap/1024);

text+=tr("系统运行时间: %1s").arg(s_info.uptime);

ui->label_RAM->setText(text);

}

QTimer::singleShot(1000, this, SLOT(GetSystemInfo()));

}

Widget::~Widget()

{

delete ui;

}

mainwindow.h代码:

#ifndef WIDGET_H

#define WIDGET_H

#include

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

private slots:

void GetSystemInfo(void);

private:

Ui::Widget *ui;

};

#endif // WIDGET_H

linux Qt

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C语言 | 奖金发放问题
下一篇:【NOIP1997】【Luogu1548】棋盘问题(枚举正方形个数)
相关文章