Files
JNetApplet/networkmonitorapplet.h
T
Jokul 011a5c7e86 feat: 实现网络速度监控任务栏插件
- 添加 C++ 后端 NetworkMonitorApplet,监控网络下载/上传速度
- 添加 QML 前端 networkview.qml,显示实时速度和流量统计
- 支持多网络接口切换
- 任务栏图标显示实时速度,高速时变色
- 悬停显示速度摘要,点击显示详细信息弹窗
- 添加安装脚本 install.sh
- 添加 .gitignore 排除构建目录
2026-07-18 10:57:23 +08:00

95 lines
2.6 KiB
C++

// SPDX-FileCopyrightText: 2024 MyCompany
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include <applet.h>
#include <QString>
#include <QStringList>
#include <QTimer>
#include <QVariantList>
#include <QMap>
DS_BEGIN_NAMESPACE
struct NetworkInterface {
QString name; // 接口名称,如 "eth0", "wlan0"
qint64 rxBytes; // 接收字节数
qint64 txBytes; // 发送字节数
qint64 rxPackets; // 接收包数
qint64 txPackets; // 发送包数
qint64 rxErrors; // 接收错误数
qint64 txErrors; // 发送错误数
qint64 rxDropped; // 接收丢弃数
qint64 txDropped; // 发送丢弃数
};
class NetworkMonitorApplet : public DApplet
{
Q_OBJECT
Q_PROPERTY(qint64 downloadSpeed READ downloadSpeed NOTIFY speedChanged)
Q_PROPERTY(qint64 uploadSpeed READ uploadSpeed NOTIFY speedChanged)
Q_PROPERTY(qint64 totalDownload READ totalDownload NOTIFY totalChanged)
Q_PROPERTY(qint64 totalUpload READ totalUpload NOTIFY totalChanged)
Q_PROPERTY(QStringList networkInterfaces READ networkInterfaces NOTIFY interfacesChanged)
Q_PROPERTY(QStringList interfaceStats READ interfaceStats NOTIFY statsChanged)
Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
Q_PROPERTY(QString activeInterface READ activeInterface NOTIFY activeInterfaceChanged)
public:
explicit NetworkMonitorApplet(QObject *parent = nullptr);
~NetworkMonitorApplet();
virtual bool load() override;
virtual bool init() override;
qint64 downloadSpeed() const;
qint64 uploadSpeed() const;
qint64 totalDownload() const;
qint64 totalUpload() const;
QStringList networkInterfaces() const;
QStringList interfaceStats() const;
bool ready() const;
QString activeInterface() const;
Q_INVOKABLE void refresh();
Q_INVOKABLE void setActiveInterface(const QString &interface);
signals:
void speedChanged();
void totalChanged();
void interfacesChanged();
void statsChanged();
void readyChanged();
void activeInterfaceChanged();
private:
void readNetworkStats();
void calculateSpeed();
void detectInterfaces();
qint64 getActiveRxBytes() const;
qint64 getActiveTxBytes() const;
QTimer *m_refreshTimer;
QMap<QString, NetworkInterface> m_interfaces;
QStringList m_interfaceList;
QString m_activeInterface;
// 速度计算
qint64 m_lastRxBytes;
qint64 m_lastTxBytes;
qint64 m_downloadSpeed;
qint64 m_uploadSpeed;
// 总量
qint64 m_totalDownload;
qint64 m_totalUpload;
bool m_ready;
bool m_firstUpdate;
};
DS_END_NAMESPACE