Qt项目实战:磁盘容量计算器
目录
一.初始化表格
二.解析从系统获取到的磁盘信息,并调用插入方法将其添加到表格中
三.向表格中插入新的行,显示指定磁盘的信息及其使用情况
四.返回控件大小
五.获取当前背景颜色
六.加载系统驱动器的信息并更新表格内容
七.效果
八.代码
1.h
2.cpp
一.初始化表格
1.设置背景颜色和各类颜色(用于进度条)。
2.清空当前表格内容。
3.设置列数为5,并定义每一列的宽度。
4.设置样式表,以调整单元格的内边距。
5.定义水平标题栏的文本。
6.设置选择行为、编辑触发器和选择模式。
7.使用 QMetaObject::invokeMethod
和 QTimer::singleShot
调用 load()
函数来加载设备信息。
二.解析从系统获取到的磁盘信息,并调用插入方法将其添加到表格中
1.将结果字符串按空格分割成多个部分,提取出设备名称、已用空间、可用空间、总大小以及使用百分比等信息。
2.如果提供了名称,则使用该名称替代提取出的设备名。
3.调用 insertSize()
方法,将这些信息插入到表格中。
三.向表格中插入新的行,显示指定磁盘的信息及其使用情况
1.在表格末尾插入新行,并创建对应的 QTableWidgetItem 对象以显示设备名称、已用空间、可用空间和总大小。
2.创建一个 QProgressBar 用于显示已用百分比,设置相应范围与值,根据百分比动态改变进度条样式(如背景色与文字颜色)。
四.返回控件大小
五.获取当前背景颜色
六.加载系统驱动器的信息并更新表格内容
1.首先清空现有的数据,通过循环移除所有行,然后通过 QDir 获取系统中的所有驱动器信息(即文件夹)。
2.对于每个驱动器,使用 Windows API 的 GetDiskFreeSpaceEx
获取该驱动器的已用空间、可用空间和总大小等信息,并将这些信息转换为合适的人类可读格式(GB)后调用之前定义的方法进行插入。
七.效果
八.代码
1.h
#ifndef DEVICESIZETABLE_H
#define DEVICESIZETABLE_H#include <QTableWidget>class QProcess;#ifdef quc
class Q_DECL_EXPORT DeviceSizeTable : public QTableWidget
#else
class DeviceSizeTable : public QTableWidget
#endif{Q_OBJECTQ_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)Q_PROPERTY(QColor chunkColor1 READ getChunkColor1 WRITE setChunkColor1)Q_PROPERTY(QColor chunkColor2 READ getChunkColor2 WRITE setChunkColor2)Q_PROPERTY(QColor chunkColor3 READ getChunkColor3 WRITE setChunkColor3)Q_PROPERTY(QColor textColor1 READ getTextColor1 WRITE setTextColor1)Q_PROPERTY(QColor textColor2 READ getTextColor2 WRITE setTextColor2)Q_PROPERTY(QColor textColor3 READ getTextColor3 WRITE setTextColor3)public:explicit DeviceSizeTable(QWidget *parent = 0);private:QProcess *process; //执行命令进程QColor bgColor; //背景颜色QColor chunkColor1; //进度颜色1QColor chunkColor2; //进度颜色2QColor chunkColor3; //进度颜色3QColor textColor1; //文字颜色1QColor textColor2; //文字颜色2QColor textColor3; //文字颜色3private slots:void readData();void checkSize(const QString &result, const QString &name);void insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent);public://默认尺寸和最小尺寸QSize sizeHint() const;QSize minimumSizeHint() const;//获取和设置背景颜色QColor getBgColor() const;void setBgColor(const QColor &bgColor);//获取和设置进度颜色1QColor getChunkColor1() const;void setChunkColor1(const QColor &chunkColor1);//获取和设置进度颜色2QColor getChunkColor2() const;void setChunkColor2(const QColor &chunkColor2);//获取和设置进度颜色3QColor getChunkColor3() const;void setChunkColor3(const QColor &chunkColor3);//获取和设置文字颜色1QColor getTextColor1() const;void setTextColor1(const QColor &textColor1);//获取和设置文字颜色2QColor getTextColor2() const;void setTextColor2(const QColor &textColor2);//获取和设置文字颜色3QColor getTextColor3() const;void setTextColor3(const QColor &textColor3);public Q_SLOTS://载入容量void load();Q_SIGNALS:void sdcardReceive(const QString &sdcardName);void udiskReceive(const QString &udiskName);
};#endif // DEVICESIZETABLE_H
2.cpp
#pragma execution_character_set("utf-8")#include "devicesizetable.h"
#include "qprocess.h"
#include "qtablewidget.h"
#include "qheaderview.h"
#include "qfileinfo.h"
#include "qdir.h"
#include "qprogressbar.h"
#include "qtimer.h"#ifdef Q_OS_WIN
#include "windows.h"
#endif
#define GB (1024 * 1024 * 1024)
#define MB (1024 * 1024)
#define KB (1024)DeviceSizeTable::DeviceSizeTable(QWidget *parent) : QTableWidget(parent)
{bgColor = QColor(255, 255, 255);chunkColor1 = QColor(100, 184, 255);chunkColor2 = QColor(24, 189, 155);chunkColor3 = QColor(255, 107, 107);textColor1 = QColor(10, 10, 10);textColor2 = QColor(255, 255, 255);textColor3 = QColor(255, 255, 255);this->clear();//设置列数和列宽this->setColumnCount(5);this->setColumnWidth(0, 100);this->setColumnWidth(1, 120);this->setColumnWidth(2, 120);this->setColumnWidth(3, 120);this->setColumnWidth(4, 120);this->setStyleSheet("QTableWidget::item{padding:0px;}");QStringList headText;headText << "盘符" << "已用空间" << "可用空间" << "总大小" << "已用百分比" ;this->setHorizontalHeaderLabels(headText);this->setSelectionBehavior(QAbstractItemView::SelectRows);this->setEditTriggers(QAbstractItemView::NoEditTriggers);this->setSelectionMode(QAbstractItemView::SingleSelection);this->verticalHeader()->setVisible(true);this->horizontalHeader()->setStretchLastSection(true);QMetaObject::invokeMethod(this, "load", Qt::QueuedConnection);QTimer::singleShot(10,this,SLOT((this->clear())));QTimer::singleShot(10, this, SLOT(load()));
}void DeviceSizeTable::readData()
{
#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
#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.isEmpty()) {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);
}QSize DeviceSizeTable::sizeHint() const
{return QSize(500, 300);
}QSize DeviceSizeTable::minimumSizeHint() const
{return QSize(200, 150);
}QColor DeviceSizeTable::getBgColor() const
{return this->bgColor;
}void DeviceSizeTable::setBgColor(const QColor &bgColor)
{if (this->bgColor != bgColor) {this->bgColor = bgColor;this->load();}
}QColor DeviceSizeTable::getChunkColor1() const
{return this->chunkColor1;
}void DeviceSizeTable::setChunkColor1(const QColor &chunkColor1)
{if (this->chunkColor1 != chunkColor1) {this->chunkColor1 = chunkColor1;this->load();}
}QColor DeviceSizeTable::getChunkColor2() const
{return this->chunkColor2;
}void DeviceSizeTable::setChunkColor2(const QColor &chunkColor2)
{if (this->chunkColor2 != chunkColor2) {this->chunkColor2 = chunkColor2;this->load();}
}QColor DeviceSizeTable::getChunkColor3() const
{return this->chunkColor3;
}void DeviceSizeTable::setChunkColor3(const QColor &chunkColor3)
{if (this->chunkColor3 != chunkColor3) {this->chunkColor3 = chunkColor3;this->load();}
}QColor DeviceSizeTable::getTextColor1() const
{return this->textColor1;
}void DeviceSizeTable::setTextColor1(const QColor &textColor1)
{if (this->textColor1 != textColor1) {this->textColor1 = textColor1;this->load();}
}QColor DeviceSizeTable::getTextColor2() const
{return this->textColor2;
}void DeviceSizeTable::setTextColor2(const QColor &textColor2)
{if (this->textColor2 != textColor2) {this->textColor2 = textColor2;this->load();}
}QColor DeviceSizeTable::getTextColor3() const
{return this->textColor3;
}void DeviceSizeTable::setTextColor3(const QColor &textColor3)
{if (this->textColor3 != textColor3) {this->textColor3 = textColor3;this->load();}
}void DeviceSizeTable::load()
{//清空原有数据int row = this->rowCount();for (int i = 0; i < row; ++i) {this->removeRow(0);}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);}}
}