//qt4递归删除文件夹
bool removeDir(const QString &dirname)
{
bool ret = true;
QDir dir(dirname);
if(dir.exists(dirname)){
QFileInfoList infolist = dir.entryInfoList();
for(int i=0;i<infolist.count();i++)
{
QFileInfo info = infolist.at(i);
if (info.fileName =="." ||info.fileName ==".." ) continue;//防止本层无限进归,这函数真坑
if(info.isDir()){
ret=removeDir(info.absoluteFilePath());
}
else{
ret = QFile::remove(info.absoluteFilePath());
}
if(!ret){
return ret;
}
}
ret = dir.rmdir(dirnane);
}
return ret;
}
下面是qt手册中的内容,总之就是entryInfoList函数会得到本层“.”和上层"..",把这俩绕开才能防止无限递归!【if (info.fileName =="." ||info.fileName ==".." ) continue;】
QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort) const
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().