一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

以下代码主要有以下功能:

  • 判断文件夹是否存在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 }
复制代码

 

posted on   一杯清酒邀明月  阅读(1917)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2021-06-01 Qt QSS QPushButton
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示