zoukankan      html  css  js  c++  java
  • Qt 删除目录

    删除目标的目录,若该目录下有子目录,一并删除。

     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         else
    16         {
    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         else
    69         {
    70             dir.rmdir(path);
    71         }
    72     }
    73 }
  • 相关阅读:
    HDU 4405 Aeroplane chess (概率dp)
    条件编译符号与公布
    hdu 1722 Cake 数学yy
    电脑显示U盘,可是读取不了
    多本Web前端深度修炼书籍(提供网盘下载链接)
    HDU 5410(2015多校10)-CRB and His Birthday(全然背包)
    Servlet体验之旅(二)——Session、Cookie
    <pre>标签
    每天学点Python之comprehensions
    编写html经常使用而又easy忘记的语句
  • 原文地址:https://www.cnblogs.com/tyche116/p/8507833.html
Copyright © 2011-2022 走看看