Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize hard disk capacity Control by Qt

Shulou Source: shulou.com Published: 2022-06-01 13:18:46 09月25日 Update

Xiaobian to share with you how Qt to achieve hard disk capacity control, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

I. Foreword

Disk capacity statistics control, to put it bluntly, is used to count the capacity occupied by the local drive letter, including but not limited to used space, remaining space, total size, used percentage, etc., where the corresponding percentage is displayed by the progress bar, the foreground color and background color of the progress bar and the text color can be set, which is needed when changing the skin as a whole. Basically, there is no difficulty in this control, that is, compatible with WIN and LINUX operating systems, winapi is used to read on WIN, linux uses QProcess to execute the corresponding command (df -h) to obtain the result, and then the timer is executed, and the amount of data returned by the associated signal slot can be analyzed. The application scenario of the control is mainly on some embedded devices, which is convenient for users to view how much space is currently remaining.

Main functions:

Total capacity/used capacity of local storage devices that can be automatically loaded

Progress bar shows used capacity

Supports all operating systems

Add USB flash drive or SD card arrival signal

void DeviceSizeTable::load(){ //Clear the original data 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, "Local Storage"); } else if (result.startsWith("/dev/mmcblk")) { checkSize(result, "Local Storage"); } else if (result.startsWith("/dev/mmcblk1p")) { checkSize(result, "SD Card"); QStringList list = result.split(" "); emit sdcardReceive(list.at(0)); } else if (result.startsWith("/dev/sd")) { checkSize(result, "USB"); 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)

The above is "Qt how to achieve hard disk capacity control" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Capacity controls space articles progress storage hard drives operating systems signals content that is data percentages systems devices statistics to put it bluntly not so much code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS Shulou Tech Info NVidia Shulou Information