#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QListWidget>
#include <QListWidgetItem>
#include <QStringList>
int a0 = 0, a1 = 0, b0 = 0, b1 = 0;
MainWindow::MainWindow(QWidget *parent) : //构造函数,初始化ui,计时器
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
QWidget::connect( timer, SIGNAL( timeout() ), this, SLOT( timer_update_currentTabInfo() ) );//ui控件-事件响应
QWidget::connect( ui->tabWidget_INFO, SIGNAL( currentChanged() ),
this, SLOT( on_tabWidget_currentChanged() ) );
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
delete timer;
}
void MainWindow::timer_update_currentTabInfo()
{
int index = ui->tabWidget_INFO->currentIndex();
if (index == 0)
{
show_tabWidgetInfo(index);
}
}
void MainWindow::show_tabWidgetInfo(int index)
{
QString tempStr; //读取文件信息字符串
QFile tempFile; //用于打开系统文件
int pos; //读取文件的位置
if (index == 0)
tempFile.setFileName("/proc/meminfo"); //打开内存信息文件
if ( !tempFile.open(QIODevice::ReadOnly) )
{
QMessageBox::warning(this, tr("warning"), tr("The meminfo file can not open!"), QMessageBox::Yes);
return ;
}
QString memTotal;
QString memFree;
QString memUsed;
QString swapTotal;
QString swapFree;
QString swapUsed;
int nMemTotal, nMemFree, nMemUsed, nSwapTotal, nSwapFree, nSwapUsed;
while (1)
{
tempStr = tempFile.readLine();
pos = tempStr.indexOf("MemTotal");
if (pos != -1)
{
memTotal = tempStr.mid(pos+10, tempStr.length()-13);
memTotal = memTotal.trimmed();
nMemTotal = memTotal.toInt()/1024;
}
else if (pos = tempStr.indexOf("MemFree"), pos != -1)
{
memFree = tempStr.mid(pos+9, tempStr.length()-12);
memFree = memFree.trimmed();
nMemFree = memFree.toInt()/1024;
}
else if (pos = tempStr.indexOf("SwapTotal"), pos != -1)
{
swapTotal = tempStr.mid(pos+11, tempStr.length()-14);
swapTotal = swapTotal.trimmed();
nSwapTotal = swapTotal.toInt()/1024;
}
else if (pos = tempStr.indexOf("SwapFree"), pos != -1)
{
swapFree = tempStr.mid(pos+10,tempStr.length()-13);
swapFree = swapFree.trimmed();
nSwapFree = swapFree.toInt()/1024;
break;
}
}
nMemUsed = nMemTotal - nMemFree;
nSwapUsed = nSwapTotal - nSwapFree;
memUsed = QString::number(nMemUsed, 10);
swapUsed = QString::number(nSwapUsed, 10);
memFree = QString::number(nMemFree, 10);
memTotal = QString::number(nMemTotal, 10);
swapFree = QString::number(nSwapFree, 10);
swapTotal = QString::number(nSwapTotal, 10);
ui->label_RAM_Used->setText(memUsed+" MB");
ui->label_RAM_Left->setText(memFree+" MB");
ui->label_RAM_Total->setText(memTotal+" MB");
ui->label_SWAP_Used->setText(swapUsed+" MB");
ui->label_SWAP_Left->setText(swapFree+" MB");
ui->label_SWAP_Total->setText(swapTotal+" MB");
ui->progressBar_RAM->setValue(nMemUsed*100/nMemTotal);
ui->progressBar_SWAP->setValue(nSwapUsed*100/nSwapTotal);
tempFile.close(); //关闭内存信息文件
int tt = 2;
int cpuInfo[2][7];
int cpuTotal[2][2];
while (tt)
{
tempFile.setFileName("/proc/stat"); //打开CPU使用状态信息
if ( !tempFile.open(QIODevice::ReadOnly) )
{
QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes);
return;
}
tempStr = tempFile.readLine();
for (int i = 0; i < 7; i++)
{
cpuInfo[2-tt][i] = tempStr.section(" ", i+1, i+1).toInt();
cpuTotal[1][2-tt] += cpuInfo[2-tt][i];
if (i == 3)
{
cpuTotal[0][2-tt] += cpuInfo[2-tt][i];
}
}
tt--;
tempFile.close(); //关闭stat文件
}
int a = cpuTotal[0][1] - cpuTotal[0][0];
int b = cpuTotal[1][1] - cpuTotal[1][0];
if (a < 0)
{
a = -a;
}
if (b < 0)
{
b = -b;
}
ui->progressBar_CPU->setValue(a*100/b);
tempFile.setFileName("/proc/stat");
//这个文件包含了CPU活动的信息
if ( !tempFile.open(QIODevice::ReadOnly) )
{
QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes);
return;
}
tempStr = tempFile.readLine();
a0 = a1;
b0 = b1;
a1 = b1 = 0;
int gg;
for (int i = 0; i < 7; i++)
{
b1 += tempStr.section(" ", i+2, i+2).toInt();
gg = b1;
if (i == 3)
{
a1 += tempStr.section(" ", i+2, i+2).toInt();
}
}
int m, n;
m = a1 - a0;
n = b1 - b0;
if (m < 0)
{
m = -m;
}
if (n < 0)
{
n = -n;
}
ui->progressBar_CPU->setValue( (n-m)*100/n );
tempFile.close(); //关闭stat文件
}
else if (index == 1) //进程信息
{
ui->listWidget_process->clear();
QDir qd("/proc");
QStringList qsList = qd.entryList();
QString qs = qsList.join("\n");
QString id_of_pro;
bool ok;
int find_start = 3;
int a, b;
int nProPid; //进程PID
int number_of_sleep = 0, number_of_run = 0, number_of_zombie = 0;
int totalProNum = 0; //进程总数
QString proName; //进程名
QString proState; //进程状态
QString proPri; //进程优先级
QString proMem; //进程占用内存
QListWidgetItem *title = new QListWidgetItem("PID\t" + QString::fromUtf8("名称") + "\t\t" +
QString::fromUtf8("状态") + "\t" +
QString::fromUtf8("优先级") + "\t" +
QString::fromUtf8("占用内存"), ui->listWidget_process);
while (1)
{
a = qs.indexOf("\n", find_start);
b = qs.indexOf("\n", a+1);
find_start = b;
id_of_pro = qs.mid(a+1, b-a-1);
totalProNum++;
nProPid = id_of_pro.toInt(&ok, 10);
if(!ok)
{
break;
}
tempFile.setFileName("/proc/" + id_of_pro + "/stat");
if ( !tempFile.open(QIODevice::ReadOnly) )
{
QMessageBox::warning(this, tr("warning"), tr("The pid stat file can not open!"), QMessageBox::Yes);
return;
}
tempStr = tempFile.readLine();
if (tempStr.length() == 0)
{
break;
}
a = tempStr.indexOf("(");
b = tempStr.indexOf(")");
proName = tempStr.mid(a+1, b-a-1);
proName.trimmed();
proState = te
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
Linux 任务管理器.rar (14个子文件)
SysMonitor
moc_mainwindow.cpp 4KB
moc_mainwindow.o 8KB
SysMonitor 93KB
SysMonitor.pro 363B
SysMonitor.pro.user.1.3 0B
ui_mainwindow.h 25KB
mainwindow.o 89KB
main.cpp 278B
mainwindow.ui 22KB
SysMonitor.pro.user 12KB
mainwindow.cpp 16KB
mainwindow.h 959B
Makefile 8KB
main.o 2KB
共 14 条
- 1
`泯~
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0