一、效果展示
二、详细代码
以C盘为例,记得加头文件
#include <QDesktopServices>
ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;
if (GetDiskFreeSpaceExA("C:\\", &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {
int totalSpaceInGBInt = static_cast<int>(totalNumberOfBytes.QuadPart / (1024LL * 1024LL * 1024.0));
int freeSpaceInGBInt = static_cast<int>(freeBytesAvailable.QuadPart / (1024LL * 1024LL * 1024.0));
ui->label_57->setText("共"+QString::number(totalSpaceInGBInt) + "GB");
ui->label_58->setText("可用" + QString::number(freeSpaceInGBInt) + "GB");
int useSpaceInGBInt = totalSpaceInGBInt - freeSpaceInGBInt;
int zhanbi = 0;
if (totalSpaceInGBInt > 0) {
double usedPercent = static_cast<double>(useSpaceInGBInt) / totalSpaceInGBInt * 100.0;
zhanbi = static_cast<int>(usedPercent);
}
if(zhanbi < 90)
{
ui->progressBar->setStyleSheet("QProgressBar::chunk { background-color: #1296db; }");
}else{
ui->progressBar->setStyleSheet("QProgressBar::chunk { background-color: red; }");
}
ui->progressBar->setValue(zhanbi);
} else {
ui->label_57->setText("无磁盘");
ui->label_58->setText(" ");
ui->progressBar->setValue(0);
}