新增CPU内存控件

master
feiyangqingyun 2023-10-17 14:11:06 +08:00
parent ac18f3530a
commit 5e1abd6353
10 changed files with 575 additions and 64 deletions

129
README.md
View File

@ -25,70 +25,71 @@
## 2 目录说明 ## 2 目录说明
**高质量项目加粗显示** **高质量项目加粗显示**
| 目录 | 相关 | 名称 | 描述 | | 目录 | 相关 | 名称 | 描述 |
| :-------: | :------: | :-----------------: | :-------------------------- | | :-------: | :------: | :-----------------: | :-------------------------- |
| control | 控件相关 | battery | **电池电量** | | control | 控件相关 | battery | **电池电量** |
| control | 控件相关 | devicebutton | **设备按钮** | | control | 控件相关 | devicebutton | **设备按钮** |
| control | 控件相关 | devicesizetable | 磁盘容量 | | control | 控件相关 | devicesizetable | 磁盘容量 |
| control | 控件相关 | imageswitch | 图片开关 | | control | 控件相关 | imageswitch | 图片开关 |
| control | 控件相关 | ipaddress | IP地址输入框 | | control | 控件相关 | ipaddress | IP地址输入框 |
| control | 控件相关 | lightbutton | 高亮按钮 | | control | 控件相关 | lightbutton | 高亮按钮 |
| control | 控件相关 | navbutton | **导航按钮** | | control | 控件相关 | navbutton | **导航按钮** |
| control | 控件相关 | savelog | **日志重定向输出** | | control | 控件相关 | savelog | **日志重定向输出** |
| control | 控件相关 | saveruntime | **运行时间记录** | | control | 控件相关 | saveruntime | **运行时间记录** |
| control | 控件相关 | smoothcurve | 平滑曲线 | | control | 控件相关 | smoothcurve | 平滑曲线 |
| control | 控件相关 | zhtopy | 汉字转拼音 | | control | 控件相关 | zhtopy | 汉字转拼音 |
| tool | 工具相关 | comtool | **串口调试助手** | | control | 控件相关 | cpumemorylabel | CPU内存控件 |
| tool | 工具相关 | nettool | **网络调试助手** | | tool | 工具相关 | comtool | **串口调试助手** |
| tool | 工具相关 | netserver | **网络中转服务器** | | tool | 工具相关 | nettool | **网络调试助手** |
| tool | 工具相关 | base64helper | **图片文字转base** | | tool | 工具相关 | netserver | **网络中转服务器** |
| tool | 工具相关 | countcode | **代码行数统计工具** | | tool | 工具相关 | base64helper | **图片文字转base** |
| tool | 工具相关 | emailtool | 邮件发送工具 | | tool | 工具相关 | countcode | **代码行数统计工具** |
| tool | 工具相关 | moneytool | 存款利息计算器 | | tool | 工具相关 | emailtool | 邮件发送工具 |
| tool | 工具相关 | pngtool | 图片警告去除工具 | | tool | 工具相关 | moneytool | 存款利息计算器 |
| tool | 工具相关 | keytool | **秘钥生成器** | | tool | 工具相关 | pngtool | 图片警告去除工具 |
| tool | 工具相关 | keydemo | 秘钥测试程序 | | tool | 工具相关 | keytool | **秘钥生成器** |
| tool | 工具相关 | livetool | 程序启动器 | | tool | 工具相关 | keydemo | 秘钥测试程序 |
| tool | 工具相关 | livedemo | 程序启动示例 | | tool | 工具相关 | livetool | 程序启动器 |
| video | 视频播放 | videobox | **视频监控布局** | | tool | 工具相关 | livedemo | 程序启动示例 |
| video | 视频播放 | videopanel | **视频监控面板** | | video | 视频播放 | videobox | **视频监控布局** |
| video | 视频播放 | videowindow | 视频监控控件 | | video | 视频播放 | videopanel | **视频监控面板** |
| video | 视频播放 | playffmpeg | 视频播放ffmpeg | | video | 视频播放 | videowindow | 视频监控控件 |
| video | 视频播放 | playvlc | 视频播放vlc | | video | 视频播放 | playffmpeg | 视频播放ffmpeg |
| video | 视频播放 | plaympv | 视频播放mpv | | video | 视频播放 | playvlc | 视频播放vlc |
| widget | 窗体相关 | colorwidget | 颜色拾取器 | | video | 视频播放 | plaympv | 视频播放mpv |
| widget | 窗体相关 | framelesswidget | **通用无边框窗体** | | widget | 窗体相关 | colorwidget | 颜色拾取器 |
| widget | 窗体相关 | gifwidget | GIF录屏 | | widget | 窗体相关 | framelesswidget | **通用无边框窗体** |
| widget | 窗体相关 | lunarcalendarwidget | **农历控件** | | widget | 窗体相关 | gifwidget | GIF录屏 |
| widget | 窗体相关 | maskwidget | 通用遮罩层 | | widget | 窗体相关 | lunarcalendarwidget | **农历控件** |
| widget | 窗体相关 | movewidget | 通用控件移动 | | widget | 窗体相关 | maskwidget | 通用遮罩层 |
| widget | 窗体相关 | screenwidget | 屏幕截图 | | widget | 窗体相关 | movewidget | 通用控件移动 |
| ui | 界面美化 | flatui | 扁平化风格 | | widget | 窗体相关 | screenwidget | 屏幕截图 |
| ui | 界面美化 | styledemo | **三套风格样式** | | ui | 界面美化 | flatui | 扁平化风格 |
| ui | 界面美化 | uidemo01 | 界面美化基础示例 | | ui | 界面美化 | styledemo | **三套风格样式** |
| ui | 界面美化 | uidemo08 | **界面美化入门示例** | | ui | 界面美化 | uidemo01 | 界面美化基础示例 |
| ui | 界面美化 | uidemo09 | 九宫格主界面 | | ui | 界面美化 | uidemo08 | **界面美化入门示例** |
| ui | 界面美化 | uidemo10 | **扁平化主界面** | | ui | 界面美化 | uidemo09 | 九宫格主界面 |
| ui | 界面美化 | iconhelper | **超级图形字体** | | ui | 界面美化 | uidemo10 | **扁平化主界面** |
| other | 其他相关 | bgdemo | 异形窗体 | | ui | 界面美化 | iconhelper | **超级图形字体** |
| other | 其他相关 | dbpage | **通用数据库翻页** | | other | 其他相关 | bgdemo | 异形窗体 |
| other | 其他相关 | echartgauge | **echart图表js交互** | | other | 其他相关 | dbpage | **通用数据库翻页** |
| other | 其他相关 | lineeditnext | 文本框回车自动跳转 | | other | 其他相关 | echartgauge | **echart图表js交互** |
| other | 其他相关 | mouseline | 鼠标十字线 | | other | 其他相关 | lineeditnext | 文本框回车自动跳转 |
| other | 其他相关 | ntpclient | NTP校时 | | other | 其他相关 | mouseline | 鼠标十字线 |
| other | 其他相关 | trayicon | 通用托盘效果 | | other | 其他相关 | ntpclient | NTP校时 |
| other | 其他相关 | multobj2slot | 多对象共用槽 | | other | 其他相关 | trayicon | 通用托盘效果 |
| other | 其他相关 | drawrect | 随机大量矩形 | | other | 其他相关 | multobj2slot | 多对象共用槽 |
| third | 第三方类 | designer | QtDesigner设计师(Qt4) | | other | 其他相关 | drawrect | 随机大量矩形 |
| third | 第三方类 | hotkey | 全局热键1 | | third | 第三方类 | designer | QtDesigner设计师(Qt4) |
| third | 第三方类 | shortcut | 全局热键2 | | third | 第三方类 | hotkey | 全局热键1 |
| third | 第三方类 | miniblink | **浏览器miniblink内核** | | third | 第三方类 | shortcut | 全局热键2 |
| third | 第三方类 | qwtdemo | 无插件qwt示例 | | third | 第三方类 | miniblink | **浏览器miniblink内核** |
| third | 第三方类 | qcustomplotdemo | **精美图表qcustomplot示例** | | third | 第三方类 | qwtdemo | 无插件qwt示例 |
| netfriend | 网友提供 | astackwidget | 动态StackWidget | | third | 第三方类 | qcustomplotdemo | **精美图表qcustomplot示例** |
| netfriend | 网友提供 | imagecropper | **头像设置工具** | | netfriend | 网友提供 | astackwidget | 动态StackWidget |
| netfriend | 网友提供 | imageviewwindow | 图片3D效果切换 | | netfriend | 网友提供 | imagecropper | **头像设置工具** |
| netfriend | 网友提供 | slidepuzzlewidget | 滑块图片验证码 | | netfriend | 网友提供 | imageviewwindow | 图片3D效果切换 |
| netfriend | 网友提供 | slidepuzzlewidget | 滑块图片验证码 |
## 3 效果图 ## 3 效果图
### 3.1 控件相关-control ### 3.1 控件相关-control
@ -114,6 +115,8 @@
![](control/0snap/smoothcurve.jpg) ![](control/0snap/smoothcurve.jpg)
- 汉字转拼音-zhtopy - 汉字转拼音-zhtopy
![](control/0snap/zhtopy.jpg) ![](control/0snap/zhtopy.jpg)
- CPU内存控件-cpumemorylabel
![](control/0snap/cpumemorylabel.jpg)
### 3.2 工具相关-tool ### 3.2 工具相关-tool
- 串口调试助手-comtool - 串口调试助手-comtool

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -9,4 +9,5 @@ SUBDIRS += navbutton
SUBDIRS += savelog SUBDIRS += savelog
SUBDIRS += saveruntime SUBDIRS += saveruntime
SUBDIRS += smoothcurve SUBDIRS += smoothcurve
SUBDIRS += zhtopy SUBDIRS += zhtopy
SUBDIRS += cpumemorylabel

View File

@ -0,0 +1,239 @@
#pragma execution_character_set("utf-8")
#include "cpumemorylabel.h"
#include "qtimer.h"
#include "qprocess.h"
#include "qdebug.h"
#ifdef Q_OS_WIN
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#endif
#include "windows.h"
#endif
#define MB (1024 * 1024)
#define KB (1024)
CpuMemoryLabel::CpuMemoryLabel(QWidget *parent) : QLabel(parent)
{
totalNew = idleNew = totalOld = idleOld = 0;
cpuPercent = 0;
memoryPercent = 0;
memoryAll = 0;
memoryUse = 0;
//获取CPU占用情况定时器
timerCPU = new QTimer(this);
connect(timerCPU, SIGNAL(timeout()), this, SLOT(getCPU()));
//获取内存占用情况定时器
timerMemory = new QTimer(this);
connect(timerMemory, SIGNAL(timeout()), this, SLOT(getMemory()));
//执行命令获取
process = new QProcess(this);
connect(process, SIGNAL(readyRead()), this, SLOT(readData()));
showText = true;
}
CpuMemoryLabel::~CpuMemoryLabel()
{
this->stop();
}
void CpuMemoryLabel::start(int interval)
{
this->getCPU();
this->getMemory();
if (!timerCPU->isActive()) {
timerCPU->start(interval);
}
if (!timerMemory->isActive()) {
timerMemory->start(interval + 1000);
}
}
void CpuMemoryLabel::stop()
{
process->close();
if (timerCPU->isActive()) {
timerCPU->stop();
}
if (timerMemory->isActive()) {
timerMemory->stop();
}
}
void CpuMemoryLabel::getCPU()
{
#ifdef Q_OS_WIN
#if 0
static FILETIME lastIdleTime;
static FILETIME lastKernelTime;
static FILETIME lastUserTime;
FILETIME newIdleTime;
FILETIME newKernelTime;
FILETIME newUserTime;
//采用GetSystemTimes获取的CPU占用和任务管理器的不一致
GetSystemTimes(&newIdleTime, &newKernelTime, &newUserTime);
int offset = 31;
quint64 a, b;
quint64 idle, kernel, user;
a = (lastIdleTime.dwHighDateTime << offset) | lastIdleTime.dwLowDateTime;
b = (newIdleTime.dwHighDateTime << offset) | newIdleTime.dwLowDateTime;
idle = b - a;
a = (lastKernelTime.dwHighDateTime << offset) | lastKernelTime.dwLowDateTime;
b = (newKernelTime.dwHighDateTime << offset) | newKernelTime.dwLowDateTime;
kernel = b - a;
a = (lastUserTime.dwHighDateTime << offset) | lastUserTime.dwLowDateTime;
b = (newUserTime.dwHighDateTime << offset) | newUserTime.dwLowDateTime;
user = b - a;
cpuPercent = float(kernel + user - idle) * 100 / float(kernel + user);
lastIdleTime = newIdleTime;
lastKernelTime = newKernelTime;
lastUserTime = newUserTime;
this->setData();
#else
//获取系统版本区分win10
bool win10 = false;
#if (QT_VERSION >= QT_VERSION_CHECK(5,4,0))
win10 = (QSysInfo::productVersion().mid(0, 2).toInt() >= 10);
#else
win10 = (QSysInfo::WindowsVersion >= 192);
#endif
QString cmd = "\\Processor(_Total)\\% Processor Time";
if (win10) {
cmd = "\\Processor Information(_Total)\\% Processor Utility";
}
if (process->state() == QProcess::NotRunning) {
process->start("typeperf", QStringList() << cmd);
}
#endif
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (process->state() == QProcess::NotRunning) {
totalNew = idleNew = 0;
process->start("cat", QStringList() << "/proc/stat");
}
#endif
}
void CpuMemoryLabel::getMemory()
{
#ifdef Q_OS_WIN
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
memoryPercent = statex.dwMemoryLoad;
memoryAll = statex.ullTotalPhys / MB;
memoryFree = statex.ullAvailPhys / MB;
memoryUse = memoryAll - memoryFree;
this->setData();
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (process->state() == QProcess::NotRunning) {
process->start("cat", QStringList() << "/proc/meminfo");
}
#endif
}
void CpuMemoryLabel::readData()
{
#ifdef Q_OS_WIN
while (!process->atEnd()) {
QString s = QLatin1String(process->readLine());
s = s.split(",").last();
s.replace("\r", "");
s.replace("\n", "");
s.replace("\"", "");
if (!s.isEmpty() && s.length() < 10) {
cpuPercent = qRound(s.toFloat());
}
}
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
while (!process->atEnd()) {
QString s = QLatin1String(process->readLine());
if (s.startsWith("cpu")) {
QStringList list = s.split(" ");
idleNew = list.at(5).toUInt();
foreach (QString value, list) {
totalNew += value.toUInt();
}
quint64 total = totalNew - totalOld;
quint64 idle = idleNew - idleOld;
cpuPercent = 100 * (total - idle) / total;
totalOld = totalNew;
idleOld = idleNew;
break;
} else if (s.startsWith("MemTotal")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryAll = s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("MemFree")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree = s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("Buffers")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree += s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("Cached")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree += s.left(s.length() - 3).toUInt() / KB;
memoryUse = memoryAll - memoryFree;
memoryPercent = 100 * memoryUse / memoryAll;
break;
}
}
#endif
this->setData();
}
void CpuMemoryLabel::setData()
{
cpuPercent = (cpuPercent < 0 ? 0 : cpuPercent);
cpuPercent = (cpuPercent > 100 ? 0 : cpuPercent);
QString msg = QString("CPU %1% Mem %2% ( 已用 %3 MB / 共 %4 MB )").arg(cpuPercent).arg(memoryPercent).arg(memoryUse).arg(memoryAll);
if (showText) {
this->setText(msg);
}
emit textChanged(msg);
emit valueChanged(cpuPercent, memoryPercent, memoryAll, memoryUse, memoryFree);
}
QSize CpuMemoryLabel::sizeHint() const
{
return QSize(300, 30);
}
QSize CpuMemoryLabel::minimumSizeHint() const
{
return QSize(30, 10);
}
bool CpuMemoryLabel::getShowText() const
{
return this->showText;
}
void CpuMemoryLabel::setShowText(bool showText)
{
this->showText = showText;
}

View File

@ -0,0 +1,75 @@
#ifndef CPUMEMORYLABEL_H
#define CPUMEMORYLABEL_H
/**
* CPU :feiyangqingyun(QQ:517216493) 2016-11-18
* 1. CPU
* 2. 使
* 3. 使
* 4. windowslinuxARM
* 5. 使便
*/
#include <QLabel>
class QProcess;
#ifdef quc
class Q_DECL_EXPORT CpuMemoryLabel : public QLabel
#else
class CpuMemoryLabel : public QLabel
#endif
{
Q_OBJECT
Q_PROPERTY(bool showText READ getShowText WRITE setShowText)
public:
explicit CpuMemoryLabel(QWidget *parent = 0);
~CpuMemoryLabel();
private:
quint64 totalNew, idleNew, totalOld, idleOld;
quint64 cpuPercent; //CPU百分比
quint64 memoryPercent; //内存百分比
quint64 memoryAll; //所有内存
quint64 memoryUse; //已用内存
quint64 memoryFree; //空闲内存
QTimer *timerCPU; //定时器获取CPU信息
QTimer *timerMemory; //定时器获取内存信息
QProcess *process; //执行命令行
bool showText; //自己显示值
private slots:
void getCPU(); //获取cpu
void getMemory(); //获取内存
void readData(); //读取数据
void setData(); //设置数据
public:
//默认尺寸和最小尺寸
QSize sizeHint() const;
QSize minimumSizeHint() const;
//获取和设置是否显示文本
bool getShowText() const;
void setShowText(bool showText);
public Q_SLOTS:
//开始启动服务
void start(int interval);
//停止服务
void stop();
Q_SIGNALS:
//文本改变信号
void textChanged(const QString &text);
//cpu和内存占用情况数值改变信号
void valueChanged(quint64 cpuPercent, quint64 memoryPercent, quint64 memoryAll, quint64 memoryUse, quint64 memoryFree);
};
#endif // CPUMEMORYLABEL_H

View File

@ -0,0 +1,17 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = cpumemorylabel
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmcpumemorylabel.cpp
SOURCES += cpumemorylabel.cpp
HEADERS += frmcpumemorylabel.h
HEADERS += cpumemorylabel.h
FORMS += frmcpumemorylabel.ui

View File

@ -0,0 +1,42 @@
#pragma execution_character_set("utf-8")
#include "frmcpumemorylabel.h"
#include "ui_frmcpumemorylabel.h"
frmCpuMemoryLabel::frmCpuMemoryLabel(QWidget *parent) : QWidget(parent), ui(new Ui::frmCpuMemoryLabel)
{
ui->setupUi(this);
this->initForm();
}
frmCpuMemoryLabel::~frmCpuMemoryLabel()
{
delete ui;
}
void frmCpuMemoryLabel::initForm()
{
QFont font;
font.setPixelSize(16);
setFont(font);
QString qss1 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("100,184,255");
QString qss2 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("255,107,107");
QString qss3 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("24,189,155");
ui->label1->setStyleSheet(qss1);
ui->label2->setStyleSheet(qss2);
ui->label3->setStyleSheet(qss3);
connect(ui->label1, SIGNAL(textChanged(QString)), ui->label2, SLOT(setText(QString)));
connect(ui->label1, SIGNAL(valueChanged(quint64, quint64, quint64, quint64, quint64)),
this, SLOT(valueChanged(quint64, quint64, quint64, quint64, quint64)));
ui->label1->start(1000);
}
void frmCpuMemoryLabel::valueChanged(quint64 cpuPercent, quint64 memoryPercent, quint64 memoryAll, quint64 memoryUse, quint64 memoryFree)
{
//重新组织文字
QString msg = QString("CPU: %1% 内存: %2% ( %3 MB / %4 MB )").arg(cpuPercent).arg(memoryPercent).arg(memoryUse).arg(memoryAll);
ui->label3->setText(msg);
}

View File

@ -0,0 +1,27 @@
#ifndef FRMCPUMEMORYLABEL_H
#define FRMCPUMEMORYLABEL_H
#include <QWidget>
namespace Ui {
class frmCpuMemoryLabel;
}
class frmCpuMemoryLabel : public QWidget
{
Q_OBJECT
public:
explicit frmCpuMemoryLabel(QWidget *parent = 0);
~frmCpuMemoryLabel();
private:
Ui::frmCpuMemoryLabel *ui;
private slots:
void initForm();
//cpu和内存占用情况数值改变信号
void valueChanged(quint64 cpuPercent, quint64 memoryPercent, quint64 memoryAll, quint64 memoryUse, quint64 memoryFree);
};
#endif // FRMCPUMEMORYLABEL_H

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmCpuMemoryLabel</class>
<widget class="QWidget" name="frmCpuMemoryLabel">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="CpuMemoryLabel" name="label1">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label2">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label3">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>CpuMemoryLabel</class>
<extends>QLabel</extends>
<header>cpumemorylabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,34 @@
#pragma execution_character_set("utf-8")
#include "frmcpumemorylabel.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font;
font.setFamily("Microsoft Yahei");
font.setPixelSize(13);
a.setFont(font);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmCpuMemoryLabel w;
w.setWindowTitle("CPU内存显示控件 (QQ: 517216493 WX: feiyangqingyun)");
w.show();
return a.exec();
}