#include "statswidget.h" #include StatsWidget::StatsWidget(QWidget *parent) : QWidget(parent) , m_statsGroup(nullptr) , m_totalCountLabel(nullptr) , m_type0CountLabel(nullptr) , m_type1CountLabel(nullptr) , m_type2CountLabel(nullptr) , m_lastUpdateLabel(nullptr) , m_totalCount(0) , m_type0Count(0) , m_type1Count(0) , m_type2Count(0) { setupUI(); applyStyles(); } StatsWidget::~StatsWidget() { } void StatsWidget::setupUI() { // 创建主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->setContentsMargins(5, 5, 5, 5); mainLayout->setSpacing(5); // 创建统计信息组 m_statsGroup = new QGroupBox("消息统计", this); QVBoxLayout *statsLayout = new QVBoxLayout(m_statsGroup); statsLayout->setContentsMargins(10, 10, 10, 10); statsLayout->setSpacing(8); // 总消息数 m_totalCountLabel = new QLabel("总消息数: 0", this); m_totalCountLabel->setObjectName("totalCountLabel"); statsLayout->addWidget(m_totalCountLabel); // 创建消息类型统计的水平布局 QHBoxLayout *typeLayout = new QHBoxLayout(); typeLayout->setSpacing(15); // 类型0消息数 m_type0CountLabel = new QLabel("类型0: 0", this); m_type0CountLabel->setObjectName("type0CountLabel"); typeLayout->addWidget(m_type0CountLabel); // 类型1消息数 m_type1CountLabel = new QLabel("类型1: 0", this); m_type1CountLabel->setObjectName("type1CountLabel"); typeLayout->addWidget(m_type1CountLabel); // 类型2消息数 m_type2CountLabel = new QLabel("类型2: 0", this); m_type2CountLabel->setObjectName("type2CountLabel"); typeLayout->addWidget(m_type2CountLabel); statsLayout->addLayout(typeLayout); // 最后更新时间 m_lastUpdateLabel = new QLabel("最后更新: --", this); m_lastUpdateLabel->setObjectName("lastUpdateLabel"); statsLayout->addWidget(m_lastUpdateLabel); // 添加到主布局 mainLayout->addWidget(m_statsGroup); mainLayout->addStretch(); setLayout(mainLayout); setMaximumHeight(150); } void StatsWidget::applyStyles() { setStyleSheet( "QGroupBox {" " font-weight: bold;" " border: 2px solid #cccccc;" " border-radius: 5px;" " margin-top: 1ex;" " padding-top: 5px;" "}" "QGroupBox::title {" " subcontrol-origin: margin;" " left: 10px;" " padding: 0 5px 0 5px;" "}" "#totalCountLabel {" " font-weight: bold;" " color: #2c3e50;" " font-size: 14px;" "}" "#type0CountLabel, #type1CountLabel, #type2CountLabel {" " color: #34495e;" " font-size: 12px;" "}" "#lastUpdateLabel {" " color: #7f8c8d;" " font-size: 11px;" "}" ); } void StatsWidget::updateStats(const QJsonObject& statsData) { qDebug() << "[StatsWidget] 收到统计更新:" << statsData; // 解析data数组并累加所有用户的统计数据 if (statsData.contains("data") && statsData["data"].isArray()) { QJsonArray dataArray = statsData["data"].toArray(); // 重置累计数据 m_totalCount = 0; m_type0Count = 0; m_type1Count = 0; m_type2Count = 0; QDateTime latestUpdate; // 遍历所有用户数据进行累加 for (const QJsonValue& value : dataArray) { if (value.isObject()) { QJsonObject userStats = value.toObject(); // 累加各类型消息数 m_totalCount += userStats["totalCount"].toInt(); m_type0Count += userStats["type0Count"].toInt(); m_type1Count += userStats["type1Count"].toInt(); m_type2Count += userStats["type2Count"].toInt(); // 找到最新的更新时间 QString lastUpdateStr = userStats["lastUpdate"].toString(); QDateTime userUpdate = QDateTime::fromString(lastUpdateStr, Qt::ISODate); if (userUpdate.isValid() && (!latestUpdate.isValid() || userUpdate > latestUpdate)) { latestUpdate = userUpdate; } } } m_lastUpdate = latestUpdate; // 更新UI显示 m_totalCountLabel->setText(QString("总消息数: %1").arg(m_totalCount)); m_type0CountLabel->setText(QString("类型0: %1").arg(m_type0Count)); m_type1CountLabel->setText(QString("类型1: %1").arg(m_type1Count)); m_type2CountLabel->setText(QString("类型2: %1").arg(m_type2Count)); if (m_lastUpdate.isValid()) { m_lastUpdateLabel->setText(QString("最后更新: %1") .arg(m_lastUpdate.toString("yyyy-MM-dd hh:mm:ss"))); } else { m_lastUpdateLabel->setText("最后更新: --"); } qDebug() << "[StatsWidget] 全局统计信息已更新 - 总数:" << m_totalCount << "类型0:" << m_type0Count << "类型1:" << m_type1Count << "类型2:" << m_type2Count << "用户数:" << dataArray.size(); } }