• 1904阅读
  • 0回复

Qt编写自定义控件34-磁盘容量统计 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-07-21

一、前言
磁盘容量统计控件,说白了,就是用来统计本地盘符占用的容量,包括但不限于已用空间、剩余空间、总大小、已用百分比等,其中对应的百分比采用进度条显示,该进度条的前景色和背景色及文字颜色可以设置,在整体换肤的时候就需要用到。
本控件的基本上没有难点可言,就是兼容WIN和LINUX操作系统,在WIN上采用winapi去读取,linux采用QProcess去执行对应的命令(df -h)获取结果,然后定时器执行,关联信号槽获取返回的额数据解析即可,控件的应用场景主要是在一些嵌入式设备上面,方便用户查看当前还剩余多少空间。

二、实现的功能
* 1:可自动加载本地存储设备的总容量/已用容量
* 2:进度条显示已用容量
* 3:支持所有操作系统
* 4:增加U盘或者SD卡到达信号

三、效果图



四、头文件代码
  1. #ifndef DEVICESIZETABLE_H
  2. #define DEVICESIZETABLE_H
  3. /**
  4. * 本地存储空间大小控件 作者:feiyangqingyun(QQ:517216493) 2016-11-30
  5. * 1:可自动加载本地存储设备的总容量/已用容量
  6. * 2:进度条显示已用容量
  7. * 3:支持所有操作系统
  8. * 4:增加U盘或者SD卡到达信号
  9. */
  10. #include <QTableWidget>
  11. class QProcess;
  12. #ifdef quc
  13. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  14. #include <QtDesigner/QDesignerExportWidget>
  15. #else
  16. #include <QtUiPlugin/QDesignerExportWidget>
  17. #endif
  18. class QDESIGNER_WIDGET_EXPORT DeviceSizeTable : public QTableWidget
  19. #else
  20. class DeviceSizeTable : public QTableWidget
  21. #endif
  22. {
  23.     Q_OBJECT
  24.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  25.     Q_PROPERTY(QColor chunkColor1 READ getChunkColor1 WRITE setChunkColor1)
  26.     Q_PROPERTY(QColor chunkColor2 READ getChunkColor2 WRITE setChunkColor2)
  27.     Q_PROPERTY(QColor chunkColor3 READ getChunkColor3 WRITE setChunkColor3)
  28.     Q_PROPERTY(QColor textColor1 READ getTextColor1 WRITE setTextColor1)
  29.     Q_PROPERTY(QColor textColor2 READ getTextColor2 WRITE setTextColor2)
  30.     Q_PROPERTY(QColor textColor3 READ getTextColor3 WRITE setTextColor3)
  31. public:
  32.     explicit DeviceSizeTable(QWidget *parent = 0);
  33. private:
  34.     QProcess *process;          //执行命令进程
  35.     QColor bgColor;             //背景颜色
  36.     QColor chunkColor1;         //进度颜色1
  37.     QColor chunkColor2;         //进度颜色2
  38.     QColor chunkColor3;         //进度颜色3
  39.     QColor textColor1;          //文字颜色1
  40.     QColor textColor2;          //文字颜色2
  41.     QColor textColor3;          //文字颜色3
  42. private slots:
  43.     void readData();
  44.     void checkSize(const QString &result, const QString &name);
  45.     void insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent);
  46. public:
  47.     QColor getBgColor()         const;
  48.     QColor getChunkColor1()     const;
  49.     QColor getChunkColor2()     const;
  50.     QColor getChunkColor3()     const;
  51.     QColor getTextColor1()      const;
  52.     QColor getTextColor2()      const;
  53.     QColor getTextColor3()      const;
  54.     QSize sizeHint()            const;
  55.     QSize minimumSizeHint()     const;
  56. public Q_SLOTS:
  57.     //载入容量
  58.     void load();
  59.     //设置背景颜色
  60.     void setBgColor(const QColor &bgColor);
  61.     //设置进度颜色
  62.     void setChunkColor1(const QColor &chunkColor1);
  63.     void setChunkColor2(const QColor &chunkColor2);
  64.     void setChunkColor3(const QColor &chunkColor3);
  65.     //设置文字颜色
  66.     void setTextColor1(const QColor &textColor1);
  67.     void setTextColor2(const QColor &textColor2);
  68.     void setTextColor3(const QColor &textColor3);
  69. Q_SIGNALS:
  70.     void sdcardReceive(const QString &sdcardName);
  71.     void udiskReceive(const QString &udiskName);
  72. };
  73. #endif // DEVICESIZETABLE_H

五、核心代码
  1. #pragma execution_character_set("utf-8")
  2. #include "devicesizetable.h"
  3. #include "qprocess.h"
  4. #include "qtablewidget.h"
  5. #include "qheaderview.h"
  6. #include "qfileinfo.h"
  7. #include "qdir.h"
  8. #include "qprogressbar.h"
  9. #include "qtimer.h"
  10. #include "qdebug.h"
  11. #ifdef Q_OS_WIN
  12. #include "windows.h"
  13. #endif
  14. #define GB (1024 * 1024 * 1024)
  15. #define MB (1024 * 1024)
  16. #define KB (1024)
  17. DeviceSizeTable::DeviceSizeTable(QWidget *parent) : QTableWidget(parent)
  18. {
  19.     bgColor = QColor(255, 255, 255);
  20.     chunkColor1 = QColor(100, 184, 255);
  21.     chunkColor2 = QColor(24, 189, 155);
  22.     chunkColor3 = QColor(255, 107, 107);
  23.     textColor1 = QColor(10, 10, 10);
  24.     textColor2 = QColor(255, 255, 255);
  25.     textColor3 = QColor(255, 255, 255);
  26.     process = new QProcess(this);
  27.     connect(process, SIGNAL(readyRead()), this, SLOT(readData()));
  28.     this->clear();
  29.     //设置列数和列宽
  30.     this->setColumnCount(5);
  31.     this->setColumnWidth(0, 100);
  32.     this->setColumnWidth(1, 120);
  33.     this->setColumnWidth(2, 120);
  34.     this->setColumnWidth(3, 120);
  35.     this->setColumnWidth(4, 120);
  36.     this->setStyleSheet("QTableWidget::item{padding:0px;}");
  37.     QStringList headText;
  38.     headText << "盘符" << "已用空间" << "可用空间" << "总大小" << "已用百分比" ;
  39.     this->setHorizontalHeaderLabels(headText);
  40.     this->setSelectionBehavior(QAbstractItemView::SelectRows);
  41.     this->setEditTriggers(QAbstractItemView::NoEditTriggers);
  42.     this->setSelectionMode(QAbstractItemView::SingleSelection);
  43.     this->verticalHeader()->setVisible(true);
  44.     this->horizontalHeader()->setStretchLastSection(true);
  45.     QTimer::singleShot(0, this, SLOT(load()));
  46. }
  47. QColor DeviceSizeTable::getBgColor() const
  48. {
  49.     return this->bgColor;
  50. }
  51. QColor DeviceSizeTable::getChunkColor1() const
  52. {
  53.     return this->chunkColor1;
  54. }
  55. QColor DeviceSizeTable::getChunkColor2() const
  56. {
  57.     return this->chunkColor2;
  58. }
  59. QColor DeviceSizeTable::getChunkColor3() const
  60. {
  61.     return this->chunkColor3;
  62. }
  63. QColor DeviceSizeTable::getTextColor1() const
  64. {
  65.     return this->textColor1;
  66. }
  67. QColor DeviceSizeTable::getTextColor2() const
  68. {
  69.     return this->textColor2;
  70. }
  71. QColor DeviceSizeTable::getTextColor3() const
  72. {
  73.     return this->textColor3;
  74. }
  75. void DeviceSizeTable::load()
  76. {
  77.     //清空原有数据
  78.     int row = this->rowCount();
  79.     for (int i = 0; i < row; i++) {
  80.         this->removeRow(0);
  81.     }
  82. #ifdef Q_OS_WIN
  83.     QFileInfoList list = QDir::drives();
  84.     foreach (QFileInfo dir, list) {
  85.         QString dirName = dir.absolutePath();
  86.         LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16();
  87.         ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
  88.         if (GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes)) {
  89.             QString use = QString::number((double)(liTotalBytes.QuadPart - liTotalFreeBytes.QuadPart) / GB, 'f', 1);
  90.             use += "G";
  91.             QString free = QString::number((double) liTotalFreeBytes.QuadPart / GB, 'f', 1);
  92.             free += "G";
  93.             QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1);
  94.             all += "G";
  95.             int percent = 100 - ((double)liTotalFreeBytes.QuadPart / liTotalBytes.QuadPart) * 100;
  96.             insertSize(dirName, use, free, all, percent);
  97.         }
  98.     }
  99. #else
  100.     process->start("df -h");
  101. #endif
  102. }
  103. void DeviceSizeTable::setBgColor(const QColor &bgColor)
  104. {
  105.     if (this->bgColor != bgColor) {
  106.         this->bgColor = bgColor;
  107.         this->load();
  108.     }
  109. }
  110. void DeviceSizeTable::setChunkColor1(const QColor &chunkColor1)
  111. {
  112.     if (this->chunkColor1 != chunkColor1) {
  113.         this->chunkColor1 = chunkColor1;
  114.         this->load();
  115.     }
  116. }
  117. void DeviceSizeTable::setChunkColor2(const QColor &chunkColor2)
  118. {
  119.     if (this->chunkColor2 != chunkColor2) {
  120.         this->chunkColor2 = chunkColor2;
  121.         this->load();
  122.     }
  123. }
  124. void DeviceSizeTable::setChunkColor3(const QColor &chunkColor3)
  125. {
  126.     if (this->chunkColor3 != chunkColor3) {
  127.         this->chunkColor3 = chunkColor3;
  128.         this->load();
  129.     }
  130. }
  131. void DeviceSizeTable::setTextColor1(const QColor &textColor1)
  132. {
  133.     if (this->textColor1 != textColor1) {
  134.         this->textColor1 = textColor1;
  135.         this->load();
  136.     }
  137. }
  138. void DeviceSizeTable::setTextColor2(const QColor &textColor2)
  139. {
  140.     if (this->textColor2 != textColor2) {
  141.         this->textColor2 = textColor2;
  142.         this->load();
  143.     }
  144. }
  145. void DeviceSizeTable::setTextColor3(const QColor &textColor3)
  146. {
  147.     if (this->textColor3 != textColor3) {
  148.         this->textColor3 = textColor3;
  149.         this->load();
  150.     }
  151. }
  152. void DeviceSizeTable::readData()
  153. {
  154.     while (!process->atEnd()) {
  155.         QString result = QLatin1String(process->readLine());
  156. #ifdef __arm__
  157.         if (result.startsWith("/dev/root")) {
  158.             checkSize(result, "本地存储");
  159.         } else if (result.startsWith("/dev/mmcblk")) {
  160.             checkSize(result, "本地存储");
  161.         } else if (result.startsWith("/dev/mmcblk1p")) {
  162.             checkSize(result, "SD卡");
  163.             QStringList list = result.split(" ");
  164.             emit sdcardReceive(list.at(0));
  165.         } else if (result.startsWith("/dev/sd")) {
  166.             checkSize(result, "U盘");
  167.             QStringList list = result.split(" ");
  168.             emit udiskReceive(list.at(0));
  169.         }
  170. #else
  171.         if (result.startsWith("/dev/sd")) {
  172.             checkSize(result, "");
  173.             QStringList list = result.split(" ");
  174.             emit udiskReceive(list.at(0));
  175.         }
  176. #endif
  177.     }
  178. }
  179. void DeviceSizeTable::checkSize(const QString &result, const QString &name)
  180. {
  181.     QString dev, use, free, all;
  182.     int percent = 0;
  183.     QStringList list = result.split(" ");
  184.     int index = 0;
  185.     for (int i = 0; i < list.count(); i++) {
  186.         QString s = list.at(i).trimmed();
  187.         if (s == "") {
  188.             continue;
  189.         }
  190.         index++;
  191.         if (index == 1) {
  192.             dev = s;
  193.         } else if (index == 2) {
  194.             all = s;
  195.         } else if (index == 3) {
  196.             use = s;
  197.         } else if (index == 4) {
  198.             free = s;
  199.         } else if (index == 5) {
  200.             percent = s.left(s.length() - 1).toInt();
  201.             break;
  202.         }
  203.     }
  204.     if (name.length() > 0) {
  205.         dev = name;
  206.     }
  207.     insertSize(dev, use, free, all, percent);
  208. }
  209. void DeviceSizeTable::insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent)
  210. {
  211.     int row = this->rowCount();
  212.     this->insertRow(row);
  213.     QTableWidgetItem *itemname = new QTableWidgetItem(name);
  214.     QTableWidgetItem *itemuse = new QTableWidgetItem(use);
  215.     itemuse->setTextAlignment(Qt::AlignCenter);
  216.     QTableWidgetItem *itemfree = new QTableWidgetItem(free);
  217.     itemfree->setTextAlignment(Qt::AlignCenter);
  218.     QTableWidgetItem *itemall = new QTableWidgetItem(all);
  219.     itemall->setTextAlignment(Qt::AlignCenter);
  220.     this->setItem(row, 0, itemname);
  221.     this->setItem(row, 1, itemuse);
  222.     this->setItem(row, 2, itemfree);
  223.     this->setItem(row, 3, itemall);
  224.     QProgressBar *bar = new QProgressBar;
  225.     bar->setRange(0, 100);
  226.     bar->setValue(percent);
  227.     QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}"
  228.                           "QProgressBar::chunk{border-radius:0px;}").arg(bgColor.name());
  229.     if (percent < 50) {
  230.         qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(chunkColor1.name());
  231.     } else if (percent < 90) {
  232.         qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(chunkColor2.name());
  233.     } else {
  234.         qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(chunkColor3.name());
  235.     }
  236.     bar->setStyleSheet(qss);
  237.     this->setCellWidget(row, 4, bar);
  238. }
  239. QSize DeviceSizeTable::sizeHint() const
  240. {
  241.     return QSize(500, 300);
  242. }
  243. QSize DeviceSizeTable::minimumSizeHint() const
  244. {
  245.     return QSize(200, 150);
  246. }

六、控件介绍
1. 超过149个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过130个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10.  集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。
14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。


七、SDK下载
- SDK下载链接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取码:877p
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo,自定义控件+属性设计器。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
- 涛哥的知乎专栏 Qt进阶之路 https://zhuanlan.zhihu.com/TaoQt
- 欢迎关注微信公众号【高效程序员】,C++/Python、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!
  
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个