網(wǎng)站首頁 編程語言 正文
一、項目介紹
本文介紹利用QDir刪除選定文件目錄下的所有空文件夾。
二、項目基本配置
新建一個Qt案例,項目名稱為“DelTest”,基類選擇“QWidget”,點擊選中創(chuàng)建UI界面復(fù)選框,完成項目創(chuàng)建。
三、UI界面設(shè)置
UI界面如下:
序號 | 名稱 | 類型 | 屬性 |
---|---|---|---|
① | pbn_del | QPushButton | text:請選擇要刪除的目錄 |
四、主程序?qū)崿F(xiàn)
4.1 widget.h頭文件
頭文件中只需聲明按鈕點擊槽函數(shù):
private slots:
void on_pbn_del_clicked();
4.2 widget.cpp源文件
定義函數(shù)checkFileOrDirExist檢查目錄是否存在:
bool checkFileOrDirExist(const QString qstrFileDirOrPath)
{
bool bRet = false;
QFileInfo objFileInfo(qstrFileDirOrPath);
if(objFileInfo.isFile())
{
bRet = objFileInfo.exists();
}
else if(objFileInfo.isDir())
{
bRet = objFileInfo.exists();
}
else
{
bRet = false;
}
return bRet;
}
定義clearEmptyFolder用于刪除空的文件夾,返回的類型為bool類型若為true則表示刪除成功,若為false則表示刪除失敗:
/**************************************************
* 功能:刪除空的文件夾
* 輸入?yún)?shù):
* qstrDirPath:文件夾路徑
* 返回值:
* bool:true -- 刪除成功,false -- 刪除失敗
* **************************************************/
bool clearEmptyFolder(const QString& qstrDirPath)
{
bool bRet = true;
do
{
if(!checkFileOrDirExist(qstrDirPath))
{
bRet = true;
break;
}
QDir qdrPath(qstrDirPath);
qdrPath.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); //設(shè)置過濾器
QFileInfoList lstFileInfo = qdrPath.entryInfoList(); //獲取所有文件信息
foreach(QFileInfo objFileInfo, lstFileInfo)
{
if(objFileInfo.isDir())
{
QString qstrSubFilePath = objFileInfo.absoluteFilePath();
clearEmptyFolder(qstrSubFilePath);
QDir qdrSubPath(qstrSubFilePath);
qdrSubPath.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
QFileInfoList qlstFileInfo = qdrSubPath.entryInfoList();
if(qlstFileInfo.count() <= 0)
{
qDebug()<<QString("remove empty dir: %1.").arg(qstrSubFilePath);
qdrSubPath.rmdir(qstrSubFilePath);
}
}
}
}while(0);
return bRet;
}
按鈕點擊槽函數(shù):
void Widget::on_pbn_del_clicked()
{
//選擇文件目錄
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
qDebug()<<dir;
bool isok=clearEmptyFolder(dir);
if(isok)
QMessageBox::information(this,"success","刪除完成");
else
QMessageBox::information(this,"error","錯誤");
}
五、效果演示
完整效果如下:
原文鏈接:https://blog.csdn.net/didi_ya/article/details/126087025
相關(guān)推薦
- 2022-12-30 React淺析Fragments使用方法_React
- 2022-10-23 Android用于加載xml的LayoutInflater源碼超詳細(xì)分析_Android
- 2022-10-15 Tomcat架構(gòu)設(shè)計及Servlet作用規(guī)范講解_Tomcat
- 2022-12-25 React?redux?原理及使用詳解_React
- 2022-11-24 GoLang切片相關(guān)問題梳理講解_Golang
- 2022-03-12 用C語言實現(xiàn)圣誕樹(簡易版+進(jìn)階版)_C 語言
- 2022-10-07 Android開發(fā)Jetpack組件Lifecycle原理篇_Android
- 2022-09-25 Spring核心IOC的核心類解析
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支