qt的磁盘剩余容量检测

void A::check()
{
    QString rootPath = "D:/d/c/f/s/d/f/g";
    if(rootPath.isEmpty())
    {
        return;
    }

    QStorageInfo storageInfo(rootPath);

    qint64 freeBytes = storageInfo.bytesAvailable();
    qint64 totalBytes = storageInfo.bytesTotal();
    double freePercentage = (double(freeBytes) / double(totalBytes)) * 100.0;
    qDebug() << "剩余空间:" << freePercentage << "%";
    if (freePercentage < 1.0)
    {
        qDebug() << "剩余空间小于 1%。退出...";
        QMessageBox::critical(nullptr, tr("磁盘空间不足"), tr("磁盘空间不足 1%"));
//        QCoreApplication::quit(); // 退出程序
    }
}
posted @ 2024-06-06 11:19  东岸  阅读(129)  评论(0)    收藏  举报