QT 编程 linux系统下获取我们的磁盘内存(总共、已用、剩余)

  • Post author:
  • Post category:linux


通过使用Qprocess 以及一些Qstring的巧妙用法来解决。

#include "widget.h"
#include "ui_widget.h"
#include <QProcess>
#include <QDebug>
#include <sys/sysinfo.h>
#include <QTimer>
 
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;
}
 
#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
 
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

1.什么是QProcess

QT提供了一个QProcess类用于启动外部程序并与之通信,我们就是利用了这个特点直接获取“df -h”的输出内容。

Qprocess有两种启动程序的方式:一体式和分离式。

一体式意味着外部程序启动以后会跟随主程序的推出而推出。

分离式则是当主程序推出时,外部程序不会理会它而是继续运行。

2.Qprocess的使用:

启动一个外部程序,需要传递外部程序的路径和执行参数参数用QStringList来带入。

    1)设置路径
    void QProcess::setProgram(const QString & program)
  2)设置参数【可选】
    void QProcess::setArguments(const QStringList & arguments)
  3)启动

3.QProcess与QIODevice

QProcess继承与QIODevice,因此,我们可以把它当作一个I/O设备进行读写操作。

4.QProcess的交互

QProcess有两种预定义的输入通道:标准输出stdout与标准错误stderr.

通过setReadChannel可以读取输出的通道

常用的读取方式有read(),readAll()或getChar(),也可以通过readAllStandardOutput()和readAllStandardError()读取标准输出和标准错误通道中的数据。

5.QProcess的同步API

QProcess提供了一系列的函数以提到事件循环来完成同步操作:

1)waitForStarted()//阻塞,直到外部程序启动

2)waitForReadyRead()//阻塞,直到输出通道中的新数据可读

3) waitForBytesWritten()//阻塞,直到输入通道中的数据被写入

4) waitForFinished() //阻塞,直到外部程序结束

注意:如果在主线程中调用这些函数,可能会造成当前用户界面不响应。

6.代码应用

        process.start("df -h");

        process.waitForFinished();

        QByteArray output = process.readAllStandardOutput();

        QString str_output = output;

str_output 中现在就储存了 df -h 程序的输出内容。接下来我们就需要对字符串进行修改分割。

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

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

这里我们通过Qstring的mid()字符串截取函数,mid(int,int n = -1),mid的第一个参数是截取字符串的起始位置,第二个参数是截取字符串的长度(不写则默认后面全部)。

indexOf(QString)是用来获取此字符串出现的第一个位置。

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

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

section()分割后的字符串按照0,1,2,3…的方式存储,-1代表倒数第一

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

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

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

    text="磁盘总容量: "+str_output.section(' ',1,1)+"\n";
    text+="已用: "+str_output.section(' ',2,2)+"\n";
    text+="可用: "+str_output.section(' ',3,3);
    text+="百分比: "str_output.section(' ',4,4).section('%',0,0).toInt()

参考:

Qt之Qprocess – 努力飞的小菜鸟 – 博客园



版权声明:本文为weixin_41558261原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。