dde-dock/plugins/plugin-guide/home_monitor/informationwidget.cpp
donghualin 73ad24a682 test: 测试插件适配新的任务栏插件接口
在homemonitor插件中新增插件新的接口,匹配新的任务栏插件

Log:
Influence: 无
Task: https://pms.uniontech.com/task-view-110309.html
Change-Id: Idd9907d55b839939d52121934bd5c28ea349392c
2022-05-25 10:59:16 +08:00

52 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "informationwidget.h"
#include <QVBoxLayout>
#include <QTimer>
#include <QDebug>
InformationWidget::InformationWidget(QWidget *parent)
: QWidget(parent)
, m_infoLabel(new QLabel)
, m_refreshTimer(new QTimer(this))
// 使用 "/home" 初始化 QStorageInfo
// 如果 "/home" 没有挂载到一个单独的分区上QStorageInfo 收集的数据将会是根分区的
, m_storageInfo(new QStorageInfo("/home"))
{
m_infoLabel->setStyleSheet("QLabel {"
"color: white;"
"}");
m_infoLabel->setAlignment(Qt::AlignCenter);
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addWidget(m_infoLabel);
centralLayout->setSpacing(0);
centralLayout->setMargin(0);
setLayout(centralLayout);
// 连接 Timer 超时的信号到更新数据的槽上
connect(m_refreshTimer, &QTimer::timeout, this, &InformationWidget::refreshInfo);
// 设置 Timer 超时为 10s即每 10s 更新一次控件上的数据,并启动这个定时器
m_refreshTimer->start(10000);
refreshInfo();
}
const QString InformationWidget::textContent() const
{
// 获取分区总容量
const double total = m_storageInfo->bytesTotal();
// 获取可用总容量
const double available = m_storageInfo->bytesAvailable();
// 得到可用百分比
const int percent = qRound(available / total * 100);
return QString("Home:\n%1\%").arg(percent);
}
void InformationWidget::refreshInfo()
{
// 更新内容
m_infoLabel->setText(textContent());
}