How to remove a non-empty folder in Qt.
You can call this Qt Remove Directory function with this method : QDir dir; dir. setPath("/your/sample/directory"); removeDirectory(dir);
To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.
Try use system " rmdir -s -q file_to_delte ". This will delete the folder and all files in it.
If you're using Qt 5, there is QDir::removeRecursively().
Recursively delete the contents of the directory first. Here is a blog post with sample code for doing just that. I've included the relevant code snippet.
bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists()) { 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 = QDir().rmdir(dirName); } return result; }
Edit: The above answer was for Qt 4. If you are using Qt 5, then this functionality is built into QDir with the QDir::removeRecursively() method .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With