在Qt中使用QDirIterator时筛选/排除目录

Filter/Exclude a directory when using QDirIterator in Qt

本文关键字:筛选 排除 QDirIterator Qt      更新时间:2023-10-16

我想知道在使用QDirIterator时是否可以排除/筛选目录。我希望它跳过它/完全忽略它。

        QString SkipThisDir = "C:stuff";
        QDirIterator CPath(PathToCopyFrom,  QDir::AllEntries | QDir::NoSymLinks, QDirIterator::Subdirectories );

            while(CPath.hasNext())
            {
                CPath.next();
                //DoSometing
            }

我在API for QDirIterator中看不到任何特定的功能。然而,像下面这样简单的事情会起作用。

while (CPath.hasNext())
{
    if (CPath.next() == SkipThisDir)
        continue;
    //DoSomething
}

首先,您必须在SkipThisDir中再添加一个反斜杠才能对其进行转义。

Second you could do a check at the beginning of the while loop and if the current folder is the one you want to skip you could continue to the next directory.
QString SkipThisDir = "C:\stuff";
QDirIterator CPath(PathToCopyFrom,  QDir::AllEntries | QDir::NoSymLinks, 
                   QDirIterator::Subdirectories );

while(CPath.hasNext())
{
    QString currentDir = CPath.next();
    if (currentDir == SkipThisDir)
         continue; 
    //DoSometing
}