liudianwu |
2020-05-17 10:46 |
Qt开源作品12-硬盘容量控件
## 一、前言 磁盘容量统计控件,说白了,就是用来统计本地盘符占用的容量,包括但不限于已用空间、剩余空间、总大小、已用百分比等,其中对应的百分比采用进度条显示,该进度条的前景色和背景色及文字颜色可以设置,在整体换肤的时候就需要用到。 本控件的基本上没有难点可言,就是兼容WIN和LINUX操作系统,在WIN上采用winapi去读取,linux采用QProcess去执行对应的命令(df -h)获取结果,然后定时器执行,关联信号槽获取返回的额数据解析即可,控件的应用场景主要是在一些嵌入式设备上面,方便用户查看当前还剩余多少空间。
主要功能: 1. 可自动加载本地存储设备的总容量/已用容量 2. 进度条显示已用容量 3. 支持所有操作系统 4. 增加U盘或者SD卡到达信号
## 二、代码思路 ```c++ void DeviceSizeTable::load() { //清空原有数据 int row = this->rowCount(); for (int i = 0; i < row; i++) { this->removeRow(0); }
#ifdef Q_OS_WIN QFileInfoList list = QDir::drives(); foreach (QFileInfo dir, list) { QString dirName = dir.absolutePath(); LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16(); ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
if (GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes)) { QString use = QString::number((double)(liTotalBytes.QuadPart - liTotalFreeBytes.QuadPart) / GB, 'f', 1); use += "G"; QString free = QString::number((double) liTotalFreeBytes.QuadPart / GB, 'f', 1); free += "G"; QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1); all += "G"; int percent = 100 - ((double)liTotalFreeBytes.QuadPart / liTotalBytes.QuadPart) * 100; insertSize(dirName, use, free, all, percent); } }
#else process->start("df -h"); #endif }
void DeviceSizeTable::readData() { while (!process->atEnd()) { QString result = QLatin1String(process->readLine()); #ifdef __arm__ if (result.startsWith("/dev/root")) { checkSize(result, "本地存储"); } else if (result.startsWith("/dev/mmcblk")) { checkSize(result, "本地存储"); } else if (result.startsWith("/dev/mmcblk1p")) { checkSize(result, "SD卡"); QStringList list = result.split(" "); emit sdcardReceive(list.at(0)); } else if (result.startsWith("/dev/sd")) { checkSize(result, "U盘"); QStringList list = result.split(" "); emit udiskReceive(list.at(0)); } #else if (result.startsWith("/dev/sd")) { checkSize(result, ""); QStringList list = result.split(" "); emit udiskReceive(list.at(0)); } #endif } }
void DeviceSizeTable::checkSize(const QString &result, const QString &name) { QString dev, use, free, all; int percent = 0; QStringList list = result.split(" "); int index = 0;
for (int i = 0; i < list.count(); i++) { QString s = list.at(i).trimmed(); if (s == "") { continue; }
index++; if (index == 1) { dev = s; } else if (index == 2) { all = s; } else if (index == 3) { use = s; } else if (index == 4) { free = s; } else if (index == 5) { percent = s.left(s.length() - 1).toInt(); break; } }
if (name.length() > 0) { dev = name; }
insertSize(dev, use, free, all, percent); }
void DeviceSizeTable::insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent) { int row = this->rowCount(); this->insertRow(row);
QTableWidgetItem *itemname = new QTableWidgetItem(name); QTableWidgetItem *itemuse = new QTableWidgetItem(use); itemuse->setTextAlignment(Qt::AlignCenter); QTableWidgetItem *itemfree = new QTableWidgetItem(free); itemfree->setTextAlignment(Qt::AlignCenter); QTableWidgetItem *itemall = new QTableWidgetItem(all); itemall->setTextAlignment(Qt::AlignCenter);
this->setItem(row, 0, itemname); this->setItem(row, 1, itemuse); this->setItem(row, 2, itemfree); this->setItem(row, 3, itemall);
QProgressBar *bar = new QProgressBar; bar->setRange(0, 100); bar->setValue(percent);
QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}" "QProgressBar::chunk{border-radius:0px;}").arg(bgColor.name());
if (percent < 50) { qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(chunkColor1.name()); } else if (percent < 90) { qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(chunkColor2.name()); } else { qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(chunkColor3.name()); }
bar->setStyleSheet(qss); this->setCellWidget(row, 4, bar); } ```
## 三、效果图 [attachment=21586]
## 四、开源主页 **以上作品完整源码下载都在开源主页,会持续不断更新作品数量和质量,欢迎各位关注。** 1. 国内站点:[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo) 2. 国际站点:[https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo) 3. 个人主页:[https://blog.csdn.net/feiyangqingyun](https://blog.csdn.net/feiyangqingyun) 4. 知乎主页:[https://www.zhihu.com/people/feiyangqingyun/](https://www.zhihu.com/people/feiyangqingyun/) |
|