ubuntu下类似360的网速测试的小软件 360网速测试

ubuntu下类似360的网速测试的小软件下面给出源码,请使用qt编译



//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;

}

ubuntu下类似360的网速测试的小软件 360网速测试
//希望对大家有帮助
//现在只是做了部分功能,简略的部分,后面我会再慢慢完善的,添加多一点实用的功能
//有兴趣的话大家也可以修改这些代码完善它,有了进展当然希望大家跟我探讨一下
欢迎转载,请注明出处.

  

爱华网本文地址 » http://www.aihuau.com/a/25101014/222816.html

更多阅读

360加速球的小秘密 精 小秘密

360加速球的小秘密 精——简介360加速球是360安全卫士默认的一个加速小工具,可以让你在网络变慢的时候通过加速球关闭一些程序来提高你的网速,360加速球还是有一些我们平时不注意的设置的,这里我们来一起看一下。360加速球的小秘密 精

ubuntu下输入法的使用 ubuntu中文输入法下载

ubuntu下输入法的使用——简介ubuntu下输入法怎么用呢?怎样更换输入方式系统(输入平台)与输入法?怎样添加删除输入法,怎样设置输入切换快捷键?下面我就把我的使用经验跟大家分享一下...ubuntu下输入法的使用——方法/步骤

显微镜下惊艳的花粉图 惊艳美女图

显微镜下惊艳的花粉(图)一、花粉过敏是怎么回事?花粉的英文名称:pollen,定义:有花植物的小孢子。萌发时产生含三个单倍体核的雄配子体。所属学科:细胞生物学(一级学科);细胞分化与发育(二级学科)。在自然界,花粉是一种主要的致敏原。每当春暖花

ubuntu下arp攻击防御和反击 ubuntu arp攻击

现在网络执法官、P2P终结者常常出现在局域网中胡搞瞎搞,使得网速变慢了,糟糕的话还断网。在windows中只要有“反p2p终结者”或arp防火墙“antiarp”,就可以轻松解决问题,在linux下呢,以下方法操作是在ubuntu下进行。用静态arp.这种方法

ubuntu下CHM阅读器 chm阅读器下载

本文旨在介绍linux下的常见chm阅读软件及其安装,并针对一些问题给出解决方法。Ubuntu自带的chm和PDF阅读器不怎么好用,功能有限,而且很卡。后来chm换了chmsee,用起来不错,可是没有搜索功能。如果是看普通的电子文档倒也没什么,可是如果是

声明:《ubuntu下类似360的网速测试的小软件 360网速测试》为网友骚先生分享!如侵犯到您的合法权益请联系我们删除