您好,登錄后才能下訂單哦!
小編給大家分享一下Qt如何實現硬盤容量控件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
磁盤容量統計控件,說白了,就是用來統計本地盤符占用的容量,包括但不限于已用空間、剩余空間、總大小、已用百分比等,其中對應的百分比采用進度條顯示,該進度條的前景色和背景色及文字顏色可以設置,在整體換膚的時候就需要用到。 本控件的基本上沒有難點可言,就是兼容WIN和LINUX操作系統,在WIN上采用winapi去讀取,linux采用QProcess去執行對應的命令(df -h)獲取結果,然后定時器執行,關聯信號槽獲取返回的額數據解析即可,控件的應用場景主要是在一些嵌入式設備上面,方便用戶查看當前還剩余多少空間。
主要功能:
可自動加載本地存儲設備的總容量/已用容量
進度條顯示已用容量
支持所有操作系統
增加U盤或者SD卡到達信號
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); }
以上是“Qt如何實現硬盤容量控件”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。