问题描述
我使用的是 VS2019,Qt 5.15.2
我在以下目录中使用 entryList
类的 QDir
方法:
带有 entryList
参数的 QDir::NodotAndDotDot
方法的以下行为很奇怪:
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
- 不要列出特殊条目“.”和“..”。