删除目标的目录,若该目录下有子目录,一并删除。
1 //判断是否存在子目录 2 bool judgeDir(QDir dir) 3 { 4 dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); 5 foreach(QFileInfo fileInfo, dir.entryInfoList()) 6 { 7 if(fileInfo.isDir()) 8 { 9 return true;10 }11 else if(fileInfo.isFile())12 {13 return true;14 }15 else16 {17 return false;18 }19 }20 return true;21 }22 23 //删除目标目录24 void delDir(QString path)25 {26 if(path.isEmpty())27 {28 QMessageBox::warning(this,tr("警告"),tr("路径为空"),QMessageBox::Yes);29 return;30 }31 QDir dir(path);32 33 dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);34 foreach(QFileInfo fileInfo, dir.entryInfoList())35 {36 if(fileInfo.isFile())37 {38 if(!fileInfo.isWritable())39 {40 QFile file(fileInfo.absoluteFilePath());41 file.setPermissions(QFile::WriteOwner);42 }43 44 fileInfo.dir().remove(fileInfo.fileName());45 46 }47 else if(fileInfo.isDir())48 {49 delDir(fileInfo.absoluteFilePath());50 }51 }52 dir.rmpath(dir.absolutePath());53 }54 55 //删除传入的目录56 void deleteFile(QString path)57 {58 QDir dir(path);59 int saveDel = QMessageBox::question(this,tr("提示"),tr("确定删除目录").arg(path), QMessageBox::Yes | QMessageBox::No );60 61 if(QMessageBox::Yes == saveDel)62 {63 QDir currentDir(path);64 if(judgeDir(currentDir))65 {66 delDir(currentDir.absolutePath());67 }68 else69 {70 dir.rmdir(path);71 }72 }73 }