当前位置:网站首页>Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
2022-08-01 12:49:00 【wendy_ya】
一、项目介绍
本文介绍利用QDir删除选定文件目录下的所有空文件夹。
二、项目基本配置
新建一个Qt案例,项目名称为“DelTest”,基类选择“QWidget”,点击选中创建UI界面复选框,完成项目创建。
三、UI界面设置
UI界面如下:
序号 | 名称 | 类型 | 属性 |
---|---|---|---|
① | pbn_del | QPushButton | text:请选择要删除的目录 |
四、主程序实现
4.1 widget.h头文件
头文件中只需声明按钮点击槽函数:
private slots:
void on_pbn_del_clicked();
4.2 widget.cpp源文件
定义函数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则表示删除失败:
/************************************************** * 功能:删除空的文件夹 * 输入参数: * 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); //设置过滤器
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;
}
按钮点击槽函数:
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://download.csdn.net/download/didi_ya/86268682
ok,以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~
边栏推荐
- How to Integrate Your Service Registry with Istio?
- Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- 论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
- leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
- 线上问题排查常用命令,总结太全了,建议收藏!!
- 透过开发抽奖小程序,体会创新与迭代
- 软件测试之发现和解决bug
- Feign 从注册到调用原理分析
- 深入解析volatile关键字
猜你喜欢
The basic knowledge of scripting language Lua summary
重磅消息 | Authing 实现与西门子低代码平台的集成
How do we do full-link grayscale on the database?
PanGu-Coder:函数级的代码生成模型
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
多线程案例——阻塞式队列
How does the SAP ABAP OData service support the Create operation trial version
Process sibling data into tree data
bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
随机推荐
How to Integrate Your Service Registry with Istio?
Process sibling data into tree data
SQL函数 %SQLUPPER
硬链接、软连接浅析
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
PanGu-Coder:函数级的代码生成模型
什么是一致性哈希?可以应用在哪些场景?
【2022蓝帽杯】file_session && 浅入opcode
批量任务导入到数据库中
SQL functions STR
数据挖掘-04
并发编程10大坑,你踩过几个?
Beyond Compare 4 trial period expires
markdown常用数学符号cov(markdown求和符号)
NebulaGraph v3.2.0 Performance Report
MVVM响应式
批量替换Word中的表格为图片并保存
Simulation implementation of new of Js handwritten function
如何使用 Authing 单点登录,集成 Discourse 论坛?
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享