以下代码主要有以下功能:
- 判断文件夹是否存在
dir.exists()
- 判断文件夹是否为空
dir.entryInfoList()
- 清空文件夹
dir.removeRecursively()
- 生成新文件夹
dir.mkpath()
1 // @brief 初始化路径(若存在且有文件,则确认是否清空)
2 // return true 成功 false 失败
3 bool MainWindow::initReportPath()
4 {
5 QString pathName = QStringLiteral("项目一路径");
6 // 建立文件夹(若不存在则建立,若存在询问用户是否清空)
7 QString reportPath = QCoreApplication::applicationDirPath() + "/DataReport/" + pathName + "/";
8
9 QDir dir;
10 bool res;
11 if (!dir.exists(reportPath))
12 {
13 res = dir.mkpath(reportPath);
14 }
15 else
16 {
17 // 已存在,判断文件夹是否为空,如果不是则提示用户是否清空
18 dir.setPath(reportPath);
19 dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
20 QFileInfoList list = dir.entryInfoList();
21
22 if (list.count() <= 0)
23 {
24 qDebug() << "文件夹为空";
25 res = true;
26 }
27 else
28 {
29 qDebug() << "文件夹不为空";
30 QMessageBox::StandardButton reply;
31 reply = QMessageBox::question(this, "提示", "目录已经存在,是否清空文件夹", QMessageBox::Yes | QMessageBox::No);
32
33 if (reply == QMessageBox::Yes)
34 {
35 dir.setPath(reportPath);
36 dir.removeRecursively();
37 res = dir.mkpath(reportPath);
38 }
39 else
40 {
41 QMessageBox::information(NULL, "提示", "请重新设置报告保存路径");
42 res = false;
43 }
44 }
45 }
46
47 if (res)
48 {
49 QMessageBox::information(this, "提示", "本次报告路径设置完成");
50 // 设置全局路径名称
51 reportPathStr = reportPath;
52 }
53
54 return res;
55 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2021-06-01 Qt QSS QPushButton