QDir entryList 函数奇怪的行为

如何解决QDir entryList 函数奇怪的行为

我使用的是 VS2019,Qt 5.15.2

我在以下目录中使用 entryList 类的 QDir 方法

enter image description here

带有 entryList 参数的 QDir::NodotAndDotDot 方法的以下行为很奇怪:

tTe 下面的例子是不言自明的: 我的代码


    LOG( INFO ) << "entryList with no parameter";
    foreach( auto elementName,p_dir.entryList() )
    {
        LOG( INFO ) << "elementName : " << elementName.toStdString();
    }

    LOG( INFO ) << "entryList with QDir::NodotAndDotDot";
    foreach( auto elementName,p_dir.entryList( QDir::NodotAndDotDot ) )
    {
        LOG( INFO ) << "elementName : " << elementName.toStdString();
    }

输出如下:

2021-04-01-10:29:33,482724 | INFO            | entryList with no parameter | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:58
2021-04-01-10:29:33,482773 | INFO            | elementName : . | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482805 | INFO            | elementName : .. | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482844 | INFO            | elementName : ABDOMEN | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482876 | INFO            | elementName : CHEST | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482913 | INFO            | elementName : CSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482944 | INFO            | elementName : ELBOW | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482985 | INFO            | elementName : FOOT | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483019 | INFO            | elementName : HAND | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483053 | INFO            | elementName : KNEE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483090 | INFO            | elementName : LSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483123 | INFO            | elementName : PELVIS | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483155 | INFO            | elementName : RIB | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483186 | INFO            | elementName : SHOULDER | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483221 | INFO            | elementName : SKULL | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483252 | INFO            | elementName : TSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483284 | INFO            | entryList with QDir::NodotAndDotDot | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:64

我不明白为什么,如果我使用过滤器 QDir::NodotAndDotDot,entryList 输出现在是空的???

解决方法

你的问题是:

我不明白为什么,如果我使用过滤器 QDir::NoDotAndDotDot

QDir::entryList 的官方文档指出 that

如果目录不可读、不存在或如果没有符合规范的内容,则返回一个空列表。

当您执行 p_dir.entryList() 时,默认过滤器为 NoFilter,因此显示所有内容是正常的。 正常行为是 NoFilter 表示显示所有内容,包括 . (Dot) 和 .. (DotDot)

当您执行 p_dir.entryList( QDir::NoDotAndDotDot ) 时,过滤器设置为 QDir::NoDotAndDotDot,这意味着 "Do not list the special entries "." and ".."."(请参阅 description of QDir::Filters,然后没有更多匹配过滤器的规范,因此,它输出一个空列表。

要显示不带 entryList with QDir::NoDotAndDotDot... 行的所有内容,您应该:

foreach( auto elementName,p_dir.entryList( QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot )
{
    LOG( INFO ) << "elementName : " << elementName.toStdString();
}

这允许显示:

  • QDir::AllDirs - 列出所有目录;即不要将过滤器应用于目录名称。
  • QDir::Files - 列出文件。
  • QDir::NoDotAndDotDot- 不要列出特殊条目“.”和“..”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?