在Qt中递归浏览目录,跳过文件夹"."并".."
Walk a directory recursively in Qt, skip the folders "." and ".."
我在使用Qt函数递归遍历目录时遇到了一些问题。我想做的事:
打开指定的目录。遍历目录,每次遇到另一个目录时,打开该目录,遍历文件等。
现在,我要怎么做:
QString dir = QFileDialog::getExistingDirectory(this, "Select directory");
if(!dir.isNull()) {
ReadDir(dir);
}
void Mainwindow::ReadDir(QString path) {
QDir dir(path); //Opens the path
QFileInfoList files = dir.entryInfoList(); //Gets the file information
foreach(const QFileInfo &fi, files) { //Loops through the found files.
QString Path = fi.absoluteFilePath(); //Gets the absolute file path
if(fi.isDir()) ReadDir(Path); //Recursively goes through all the directories.
else {
//Do stuff with the found file.
}
}
}
现在,我面临的实际问题是:entryInfoList自然也会返回"answers".."目录。通过这种设置,这证明了一个主要问题。
通过进入".",它会遍历整个目录两次,甚至无限次(因为'.'总是第一个元素),带有'.'它将为父目录下的所有文件夹重做该过程。
我想做这件漂亮而时尚的事,有什么办法吗,我不知道?或者是唯一的方法,我得到一个普通的文件名(没有路径),并根据"进行检查和"…"?
您应该尝试在entryInfoList
中使用QDir::NoDotAndDotDot
筛选器,如文档中所述。
编辑
-
不要忘记添加
QDir::Files
、QDir::Dirs
或QDir::AllFiles
来获取文件和/或目录,如本文所述。 -
您可能还想检查一下前面的问题。
相关文章:
- 如何识别项目是 QT 中的文件还是文件夹
- 如何在窗口中显示文件夹中所有文件的名称?Qt c++.
- Qt TreeView获取总行数,考虑展开和折叠的文件夹
- 移动了 QT 项目,重命名文件夹和项目名称,Moc'ing 返回"'-I'之后缺少值"
- 如何在QT中获取Windows默认文件夹的本地名称
- Qt C++:将所有库文件包含在项目文件夹中
- 在x元素之后,从QT中删除x元素之后的文件夹中的项目
- 如何从文件夹中获取文件名,并将它们添加到QT中的孩子中
- QT:在GetSaveFileName中隐藏文件夹
- 遍历指定文件夹的目录?Qt 和 c++
- Qt在文件夹之间创建链接
- Qt 5 将 Qt5Core.dll 放在一个文件夹中
- 在Qt中递归浏览目录,跳过文件夹"."并".."
- Qt Creator在指定的调试文件夹中创建调试和发布文件夹
- 在qt中遍历文件夹中的文件非常慢
- Qt自动制作独立的include、ui和资源文件夹
- 使用 Qt 从文件夹中加载图像
- Qmake Qt:将头文件移动到构建文件夹
- 将Qt DLL保存在单独的文件夹中
- 为什么Qt在Qt内部运行时或调试时不会创建文本文件,而是直接从调试文件夹创建文本文件