Removing a folder and all its contents with Qt?

How to delete a folder and all its contents using Qt ?

I tried using:

 QFile::remove(); 

but it seems that it only deletes one file at a time.

+5
source share
1 answer

For Qt5 there is QDir :: removeRecursively :

 QDir dir("C:\\Path\\To\\Folder\\Here"); dir.removeRecursively(); 

For Qt4 or lower, you can use a recursive function that deletes each file:

 bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir()) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; } 

as indicated here .

+15
source

Source: https://habr.com/ru/post/1210322/


All Articles