//networkspeed.h//以下为头文件内容//读取数据原理:读取ubuntu下/proc内的实时文件,其实相当与在读取实时内存信息
#ifndef NETWORKSPEED_H
#define NETWORKSPEED_H
#include"QTextEdit"
#include"QLabel"
#include"QPushButton"
#include<QString>
#include <QtGui/QWidget>
#include<QtGui/QMainWindow>
class netWorkSpeed : public QWidget
{
Q_OBJECT
public:
netWorkSpeed(QWidget *parent = 0);
~netWorkSpeed();
void readcpu();//读取cpu数据
void readmem();//读取内存数据
void readnet();//读取流量,计算网速
private:
QPushButton *label1; //使用率
QPushButton *labMemTotal;//总内存
QPushButton *labFree; //空余内存
QPushButton *speedButton;
QPushButton *exitbutton;
QPoint windowPos;
QPoint mousePos;
QPoint dPos;
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void changeEvent(QEvent *e);
private slots:
void readsysteminfo();
void closewin();
void showde();//弹出详细信息
void showmem();//弹出内存详细信息
void showcpu();//弹出cpu详细信息
};
#endif // NETWORKSPEED_H
//networkspeed.cpp
//源文件
#include "networkspeed.h"
#include"QFileDialog"
#include"QVBoxLayout"
#include"QPushButton"
#include"QTextStream"
#include"QApplication"
#include"QTimer"
#include"QMouseEvent"
netWorkSpeed::netWorkSpeed(QWidget *parent)
: QWidget(parent)
{
QFile file(":/qss/abc.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(styleSheet);
file.close();
exitbutton = new QPushButton("MaKer: Uubntu爱好者杨");//单击此按钮关闭窗体
connect(exitbutton,SIGNAL(clicked()),this,SLOT(closewin()));
exitbutton->setVisible(false);
exitbutton->setMinimumSize(200,20);
exitbutton->setMaximumSize(200,20);
label1 = new QPushButton();
connect(label1,SIGNAL(clicked()),this,SLOT(showmem()));
label1->setVisible(false);
label1->setMaximumSize(200,20);
label1->setMinimumSize(200,20);
labMemTotal = new QPushButton();
labMemTotal->setVisible(false);
labMemTotal->setMaximumSize(200,20);
labMemTotal->setMinimumSize(200,20);
labFree = new QPushButton();
labFree->setVisible(false);
labFree->setMaximumSize(200,20);
labFree->setMinimumSize(200,20);
speedButton= new QPushButton();
speedButton->setMaximumSize(200,20);
speedButton->setMinimumSize(200,20);
connect(speedButton,SIGNAL(clicked()),this,SLOT(showde()));
QTimer *time1 = new QTimer();
connect(time1,SIGNAL(timeout()),this,SLOT(readsysteminfo()));
time1->start(1000);//每隔一秒刷新一次
QVBoxLayout *layout1 = new QVBoxLayout();
layout1->addWidget(exitbutton);
layout1->addWidget(labMemTotal);
layout1->addWidget(labFree);
layout1->addWidget(label1);
layout1->addWidget(speedButton);
this->move(1200,600);
this->setLayout(layout1);
}
void netWorkSpeed::readmem()//读取内存信息
{
//QString fileName= QFileDialog::getOpenFileName(this, "Open system File", "", "System File(*.text);;Text(*.plain)");
QString fileName="/proc/meminfo";
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
return ;
}
QTextStream stream(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
QString fileText;
QStringList list1;
do {
fileText =stream.readLine();
list1+=fileText;
} while (!fileText.isNull());
QApplication::restoreOverrideCursor();
file.close();
int a=1;
int b =1;
double c =0;
QString str1 = list1.at(0);
str1 = str1.mid(17,8);
a= str1.trimmed().toInt()/1024;//总内存
str1 = str1.setNum(a)+" MB";
labMemTotal->setText("MemTotal: "+str1);
QString str2 = list1.at(1);
str2 = str2.mid(17,8);
b= str2.trimmed().toInt()/1024;//剩余内存
str2 = str2.setNum(b)+" MB";
labFree->setText("MemFree: "+str2);
c = (double(a-b)/a)*100;
QString labstr=NULL;
labstr.setNum(c);
label1->setText("MemUsed: "+labstr+" %");
}
void netWorkSpeed::readcpu()
{
}
bool readok = false;
int aa=1;
int bb=1;
void netWorkSpeed::readnet()
{
QString fileName="/proc/net/dev";
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
return ;
}
QTextStream stream(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
QString fileText;
QStringList list1;
do {
fileText =stream.readLine();
list1+=fileText;
} while (!fileText.isNull());
QApplication::restoreOverrideCursor();
file.close();
int a=1;
int b =1;
int aaa=0;
int bbb=0;
QString str1 = list1.at(5); //第6行eth0的数据
if(readok) //初始化缓冲第一次数据,后一次减去前一次的流量
{
str1 = str1.mid(8,10); //接受流量
a= str1.trimmed().toInt();
aaa=a;
a=(a-aa)/1024; //后一次减去前一次的流量
aa=aaa;
str1 = str1.setNum(a);
}
else
{
str1 = str1.mid(8,10);//接受流量
aa= str1.trimmed().toInt();
str1 = str1.setNum(0);
//readok = true;
}
QString str2 = list1.at(5);
if(readok) //初始化缓冲第一次数据,后一次减去前一次的流量
{
str2 = str2.mid(65,10); //接受流量
b= str2.trimmed().toInt();
bbb=b;
b=(b-bb)/1024; //后一次减去前一次的流量
bb=bbb;
str2 = str2.setNum(b);
}
else
{
str2 = str2.mid(65,10);//接受流量
bb= str2.trimmed().toInt();
str2 = str2.setNum(0);
readok = true;
}
// str2 = str2.mid(65,10);
// b= str2.trimmed().toInt()/1024;//剩余内存
// str2 = str2.setNum(b);
QString speestr = NULL;
speestr+="↓ ";
speestr+=str1;
speestr+=" KB/s | ";
speestr+="↑ ";
speestr+=str2;
speestr+=" KB/s";
speedButton->setText(speestr);
}
void netWorkSpeed::readsysteminfo()
{
readmem();
readnet();
readcpu();
}
void netWorkSpeed::mousePressEvent(QMouseEvent *event)
{
this->windowPos = this->pos();
this->mousePos = event->globalPos();
this->dPos = mousePos - windowPos;
}
void netWorkSpeed::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - this->dPos);
}
void netWorkSpeed::changeEvent(QEvent *e)
{
// QWidget::changeEvent(e);
// switch (e->type())
// {
// case QEvent::LanguageChange:
// break;
// default:
// break;
// }
}
static int shownum = 1;//决定是否show出详细信息
//弹出简略信息
void netWorkSpeed::showde()
{
if(shownum==-1)
{
label1->setVisible(false);
exitbutton->setVisible(false);
labMemTotal->setVisible(false);
labFree->setVisible(false);
shownum=shownum*(-1);
}
else
{
label1->setVisible(true);
exitbutton->setVisible(true);
shownum=shownum*(-1);
}
}
//弹出内存信息
static int shownum2 = 1;
void netWorkSpeed::showmem()
{
if(shownum2==-1)
{
labMemTotal->setVisible(false);
labFree->setVisible(false);
shownum2=shownum2*(-1);
}
else
{
labMemTotal->setVisible(true);
labFree->setVisible(true);
shownum2=shownum2*(-1);
}
}
//弹出cpu信息
void netWorkSpeed::showcpu()
{
}
void netWorkSpeed::closewin()
{
this->close();
}
netWorkSpeed::~netWorkSpeed()
{
}
//想要有酷炫的效果就要用到样式表
//样式表文件
//abc.qss
//样式表的使用本人在另一篇博文讲过,大家可以参考http://blog.sina.com.cn/s/blog_807916e0010159qj.html
//下面是本程序用到的样式表代码
*{
font-size:13px;
color:white;
font-family:"宋体";
}
CallWidget QLineEdit#telEdt
{
font-size:24px;
}
QMainWindow,QDialog{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #1B2534, stop: 0.4 #010101,
stop: 0.5 #000101, stop: 1.0 #1F2B3C);
}
QWidget{
background:#121922;
}
QLabel{
background:transparent;
}
DailForm QLineEdit#phoneLineEdt{
font-size:36px;
font-weight: bold;
}
QPushButton,QToolButton{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #5B5F5F, stop: 0.5 #0C2436,
stop: 1.0 #27405A);
border-style: outset;
border-width: 1px;
border-radius: 5px;
border-color: #11223F;
padding: 1px;
}
QPushButton::hover,QToolButton::hover{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #758385, stop: 0.5 #122C39,
stop: 1.0 #0E7788);
border-color: #11505C;
}
QPushButton::pressed,QToolButton::pressed{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #969B9C, stop: 0.5 #16354B,
stop: 1.0 #244F76);
border-color: #11505C;
}
QPushButton::disabled,QToolButton::disabled{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #282B2C, stop: 0.5 #09121A,
stop: 1.0 #111D29);
border-color: #0A1320;
color:#6A6864;
}
//希望对大家有帮助
//现在只是做了部分功能,简略的部分,后面我会再慢慢完善的,添加多一点实用的功能
//有兴趣的话大家也可以修改这些代码完善它,有了进展当然希望大家跟我探讨一下
欢迎转载,请注明出处.