-
前言
一、首先是pro文件
二、头文件(.h)
三、cpp文件(注意这不是main.cpp文件)
四、ui
总结
前言
该项目在linux下运用QT5.9进行编程,实现了实时监控cpu、mem、swap和网速的变化
本篇文章仅供本人备忘用
提示:以下是本篇文章正文内容,下面案例可供参考
一、首先是pro文件
需要在pro文件中加入charts
QT += core gui charts greaterThan(QT_MAJOR_VERSION, 4): QT += widgets ConFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp pie.cpp HEADERS += pie.h FORMS += pie.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
二、头文件(.h)
导入相关的头文件,以及声明函数及变量
#ifndef PIE_H #define PIE_H #include三、cpp文件(注意这不是main.cpp文件)#include #include #include #include QT_BEGIN_NAMESPACE namespace Ui { class Pie; } QT_END_NAMESPACE class Pie : public QMainWindow { Q_OBJECT public: Pie(QWidget *parent = nullptr); ~Pie(); void CPU_Pie_chart(); void Mem_pie_chart(); void Swap_pie_chart(); void Line_chart(); void set_CPU(float id); //设置cpu饼图数据 void set_Mem(int Memid,int Memuse);//设置Mem饼图数据 void set_Swap(int Swapid,int Swapuse);//设置Swap饼图数据 void Process_init(); //饼图进程初始化 void Process_init_Net();//折线图进程初始化 //翻页按钮 void pushButton01(); void pushButton02(); void pushButton03(); void set_Net(int X1,float y1,int x2,float y2);//设置折线图数据 public slots: void readRead(); void Msg_timer_Fun(); void Msg_timer_Fun_Net(); private: Ui::Pie *ui; QProcess *process;//饼图进程 QProcess *processNet;//折线图进程 QPieSeries *CPU_series ; //Cpu饼图数据 QByteArray msg;//char型字符 饼图信息 QByteArray msgNet;//char字符 折线图信息 QChart * CPU_chart ; //CPU饼图 QTimer Msg_timer; //定时器 //Mem QPieSeries *Mem_series;//Men饼图数据 QChart *Mem_chart ;//Mem饼图 //Swap QPieSeries *Swap_series;//Swap饼图数据 QChart *Swap_chart ;//Swap饼图 //折线图 QChart *Net_chart;//折线图 QLineSeries *Net_serial_rx; QLineSeries *Net_serial_tx; QValueAxis *axisX; QValueAxis *axisY; uint rx_bytes; uint tx_bytes; uint last_rx_bytes; uint last_tx_bytes; uint rx_rate; uint tx_rate; float rx; float tx; //QVector是QT中容器中的一种 对象存储在连续的内存中,可以使用索引号访问 //QPointF类使用浮点数定义平面中的点 QPoint就是整数 QVector Net_data_rx; QVector Net_data_tx; }; #endif // PIE_H
#include "pie.h" #include "ui_pie.h" Pie::Pie(QWidget *parent) : QMainWindow(parent) , ui(new Ui::Pie) { ui->setupUi(this); //设置开始页面为第一页 ui->stackedWidget->setCurrentIndex(0); //创建CPU饼图 CPU_Pie_chart(); //创建Mcm饼图 Mem_pie_chart(); //创建Swap饼图 Swap_pie_chart(); //创建折线图 Line_chart(); //饼图进程初始化 Process_init(); //折线图进程初始化 Process_init_Net(); connect(ui->btn1, &QPushButton::clicked,this, &Pie::pushButton01); connect(ui->btn2, &QPushButton::clicked,this, &Pie::pushButton02); connect(ui->btn3, &QPushButton::clicked,this, &Pie::pushButton03); } //饼图进程初始化 void Pie::Process_init() { //新建饼图进程 process = new QProcess(); //数据接收槽函数连接 connect(process, SIGNAL(readyRead()), this, SLOT(readRead())); //禁止当前进程,使其立即退出 process->kill(); //开始进行查询指令 process->start("top -b -n 1"); //资源、进程定时查询 Msg_timer.setInterval(1000);//1000ms触发一次每一秒 //连接定时器定时触发信号和槽函数 connect(&Msg_timer,SIGNAL(timeout()),this,SLOT(Msg_timer_Fun())); //启动定时器 Msg_timer.start(); } //折线图进程初始化 void Pie::Process_init_Net(){ //新建折线图进程 processNet = new QProcess; //杀掉进程 processNet->kill();//杀掉进程 //开始查询 processNet->start("cat /proc/net/dev"); QByteArray msg = processNet->readAll(); QString str(msg); //换行符分割 QStringList msgList = str.split(QRegExp("[rn]"),QString::SkipEmptyParts); for(int i=2;i四、uikill();//杀掉进程 processNet->start("cat /proc/net/dev"); //等待获取信息完成 processNet->waitForFinished(); QByteArray msgNet = processNet->readAll(); //记录上一次的点 last_rx_bytes = rx_bytes; last_tx_bytes = tx_bytes; QString str(msgNet); QStringList msgList = str.split(QRegExp("[rn]"),QString::SkipEmptyParts); for(int i=3;i setUseOpenGL(true); //设置控件外边界全部为0 CPU_chart->layout()->setContentsMargins(0, 0, 1, 0); //设置内边界全部为0 CPU_chart->setMargins(QMargins(0, 0, 0, 0)); //设置背景区域无圆角 CPU_chart->setBackgroundRoundness(0); //设置白色主题 CPU_chart->setTheme(QChart::ChartThemeLight); //设置背景阴影 CPU_chart->setDropShadowEnabled(true); //把饼图到QChart上 CPU_chart->addSeries(CPU_series); //设置标题画笔 CPU_chart->setTitleBrush(QBrush(QColor(0, 0, 255))); //设置标题字体 CPU_chart->setTitleFont(QFont("微软雅黑")); //设置标题 CPU_chart->setTitle("CPU使用情况"); //隐藏说明 CPU_chart->legend()->hide(); //把CPU_chart添加到视图中 ui->charCPU_view->setChart(CPU_chart); //设置QPainter::Antialiasing 绘图抗锯齿 ui->charCPU_view->setRenderHint(QPainter::Antialiasing); } //Mem饼图创建 void Pie::Mem_pie_chart() { //分配新对象 Mem_chart = new QChart(); Mem_series = new QPieSeries(Mem_chart); //启动OpenGL加速 Mem_series->setUseOpenGL(true); //设置控件外边界全部为0 Mem_chart->layout()->setContentsMargins(0, 0, 1, 0); //设置内边界全部为0 Mem_chart->setMargins(QMargins(0, 0, 0, 0)); //设置背景区域无圆角 Mem_chart->setBackgroundRoundness(0); //设置白色主题 Mem_chart->setTheme(QChart::ChartThemeLight); //设置背景阴影 Mem_chart->setDropShadowEnabled(true); //把饼图到QChart上 Mem_chart->addSeries(Mem_series); //设置标题画笔 Mem_chart->setTitleBrush(QBrush(QColor(0, 0, 255))); //设置标题字体 Mem_chart->setTitleFont(QFont("微软雅黑")); //设置标题 Mem_chart->setTitle("Mem使用情况"); //隐藏说明 Mem_chart->legend()->hide(); //把Mem_chart添加到视图中 ui->charMem_view->setChart(Mem_chart); //设置QPainter::Antialiasing 绘图抗锯齿 ui->charMem_view->setRenderHint(QPainter::Antialiasing); } //Swap饼图创建 void Pie::Swap_pie_chart() { //分配新对象 Swap_chart = new QChart(); Swap_series = new QPieSeries(Swap_chart); //启动OpenGL加速 Swap_series->setUseOpenGL(true); //设置控件外边界全部为0 Swap_chart->layout()->setContentsMargins(0, 0, 1, 0); //设置内边界全部为0 Swap_chart->setMargins(QMargins(0, 0, 0, 0)); //设置背景区域无圆角 Swap_chart->setBackgroundRoundness(0); //设置白色主题 Swap_chart->setTheme(QChart::ChartThemeLight); //设置背景阴影 Swap_chart->setDropShadowEnabled(true); //把饼图到QChart上 Swap_chart->addSeries(Swap_series);//添加数据 //设置标题画笔 Swap_chart->setTitleBrush(QBrush(QColor(0, 0, 255))); //设置标题字体 Swap_chart->setTitleFont(QFont("微软雅黑")); //设置标题 Swap_chart->setTitle("Swap使用情况"); //隐藏说明 Swap_chart->legend()->hide(); //把Swap_chart添加到视图中 ui->charSwap_view->setChart(Swap_chart); //设置QPainter::Antialiasing 绘图抗锯齿 ui->charSwap_view->setRenderHint(QPainter::Antialiasing); } //Line折线图创建 void Pie::Line_chart(){ //创建折线对象 Net_chart = new QChart(); Net_serial_rx = new QLineSeries(); Net_serial_tx = new QLineSeries(); //创建 x y 坐标轴 QValueAxis* axisX = new QValueAxis; QValueAxis* axisY = new QValueAxis; //X轴标签 axisX->setTitleText("时间(S)"); //X Y轴格子数 axisX->setTickCount(10); axisY->setTickCount(8); //将轴添加到图表中,并且需要对齐 Net_chart->addAxis(axisX,Qt::AlignBottom); Net_chart->addAxis(axisY,Qt::AlignLeft); //将数据添加到图表中 Net_chart->addSeries(Net_serial_rx); Net_chart->addSeries(Net_serial_tx); //数据必须与坐标轴对齐 Net_serial_rx->attachAxis(axisX); Net_serial_rx->attachAxis(axisY); Net_serial_tx->attachAxis(axisX); Net_serial_tx->attachAxis(axisY); //设置x轴的范围 Net_chart->axisX()->setRange(0,60); Net_chart->axisY()->setRange(0,20); //将折线图放在line_view视图当中 ui->line_view->setChart(Net_chart); } //设置Line折线图数据 void Pie::set_Net(int x1,float y1,int x2,float y2){ //在存储数据中0处插入点数据 Net_data_rx.insert(0,QPointF(x1,y1)); Net_data_tx.insert(0,QPointF(x2,y2)); //让每个点的x坐标加1; for(int i=1;i replace(Net_data_rx); Net_serial_tx->replace(Net_data_tx); } //设置cpu饼图数据 void Pie::set_CPU(float id) { //清除数据 CPU_series->clear(); //添加饼图标签和百分值 CPU_series->append(QString("使用:%1 %").arg(100-id),100-id); CPU_series->append(QString("空闲:%1 %").arg(id), id); //设置第0快饼颜色 CPU_series->slices().at(0)->setColor(QColor(13, 128, 217)); CPU_series->slices().at(0)->setLabelColor(QColor(13, 128, 217)); //设置第1快饼颜色 CPU_series->slices().at(1)->setColor(QColor(69, 13, 217)); CPU_series->slices().at(1)->setLabelColor(QColor(69, 13, 217)); //设置label显示 ui->CpuUse->setText(QString(" CPU使用:%1 %").arg(100-id)); ui->CpuIdle->setText(QString("CPU空闲:%1 %").arg(id)); } //设置Mem饼图数据 void Pie::set_Mem(int Memid,int Memuse) { //清除数据 Mem_series->clear(); //添加饼图标签和百分值 Mem_series->append(QString("空闲:%1 M").arg(Memid),Memid); Mem_series->append(QString("使用:%1 M").arg(Memuse),Memuse); //设置第0快饼颜色 Mem_series->slices().at(0)->setColor(QColor(13, 128, 217)); Mem_series->slices().at(0)->setLabelColor(QColor(13, 128, 217)); //设置第1快饼颜色 Mem_series->slices().at(1)->setColor(QColor(69, 13, 217)); Mem_series->slices().at(1)->setLabelColor(QColor(69, 13, 217)); //设置label显示 ui->MemUse->setText(QString("Mem使用:%1 M").arg(Memuse)); ui->MemIdle->setText(QString("Mem空闲:%1 M").arg(Memid)); } //设置Swap饼图数据 void Pie::set_Swap(int Swapid,int Swapuse) { //清除数据 Swap_series->clear(); //添加饼图标签和百分值 Swap_series->append(QString("使用:%1 M").arg(Swapuse),Swapuse); Swap_series->append(QString("空闲:%1 M").arg(Swapid), Swapid); //设置第0快饼颜色 Swap_series->slices().at(0)->setColor(QColor(13, 128, 217)); Swap_series->slices().at(0)->setLabelColor(QColor(13, 128, 217)); //设置第1快饼颜色 Swap_series->slices().at(1)->setColor(QColor(69, 13, 217)); Swap_series->slices().at(1)->setLabelColor(QColor(69, 13, 217)); //设置label显示 ui->SwapUse->setText(QString("Swap使用:%1 M").arg(Swapuse)); ui->SwapIdle->setText(QString("Swap剩余:%1 M").arg(Swapid)); } //定时处理和查询资源状态 void Pie::Msg_timer_Fun()//获取和分析数据 { //把获取到的数据转换为QString QString str(msg);//强制转换字符串 //换行符分割 QStringList msgList = str.split(QRegExp("[rn]"),QString::SkipEmptyParts);//split只能分割字符串 //空格符分割 QStringList CpuList = msgList[2].split(QRegExp("\s+") , QString::SkipEmptyParts); //设置CPU数据 set_CPU(CpuList[7].toFloat()); //空格符分割 QStringList MemList = msgList[3].split(QRegExp("\s+") , QString::SkipEmptyParts); //设置Mem数据 set_Mem(MemList[5].toFloat(),MemList[7].toFloat()); //空格符分割 QStringList SwapList = msgList[4].split(QRegExp("\s+") , QString::SkipEmptyParts); //设置Swap数据 set_Swap(SwapList[4].toFloat(),SwapList[6].toFloat()); //开启新一次资源查询 msg.clear(); process->kill(); process->start("top -b -n 1"); } //读取数据 void Pie::readRead() { msg += process->readAll();//readAll从设备读取所有剩余数据,并将其作为字节数组返回 } //跳转页面 void Pie::pushButton01(){ ui->stackedWidget->setCurrentIndex(0); } void Pie::pushButton02(){ ui->stackedWidget->setCurrentIndex(1); } void Pie::pushButton03(){ ui->stackedWidget->setCurrentIndex(2); } Pie::~Pie() { delete ui; }
加入四个Graphics View,分别命名为charCPU_view、charMem_view、charSwap_view、line_view。三个饼图都各加两个Label分别命名为CpuIdle、CpuUse、MemIdle、MemUse、SwapIdle、SwapUse。
最重要的要把三个饼图和折线图进行提升,自己手动提升
总结
最终的运行如下
代码还提供了按钮翻页功能,由于翻页后的内容内有进行编写就不过多提及
重要的事情说三遍
仅供本人参考、备忘!!!
仅供本人参考、备忘!!!
仅供本人参考、备忘!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)