Qt获取目录大小
#include <QDir> #include <QFileInfoList> #include <QDebug> qint64 getDirectorySize(const QString &dirPath) { qint64 size = 0; QDir dir(dirPath); QFileInfoList fileList = dir.entryInfoList(QDir::Files); foreach (QFileInfo fileInfo, fileList) { size += fileInfo.size(); } QFileInfoList dirList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); foreach (QFileInfo dirInfo, dirList) { size += getDirectorySize(dirInfo.filePath()); } return size; } int main() { QString dirPath = "/path/to/your/directory"; qint64 dirSize = getDirectorySize(dirPath); qDebug() << "Directory size:" << dirSize << "bytes"; return 0; }