6
使用Qt函数递归遍历目录时遇到了一些麻烦。 我在做什么:在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也会返回'。'和'..'目录。通过这种设置,这证明了一个主要问题。
通过进入'。',它会遍历整个目录两次,甚至无限(因为'。'始终是第一个元素),使用'..'它将重做进程中的所有文件夹父目录。
我想这样做很好,圆滑,有没有什么办法可以解决这个问题,我不知道?或者是唯一的方法,我得到纯文件名(没有路径),并检查'。'。和'..'?
是的,并如所述[这里](http://www.qtcentre.org/threads/19085-QDir-entryInfoList-and-NoDotAndDotDot-filter)时,QT必须的QDir,和过滤器需要用QDir :: AllEntries进行扩展。你仍然得到'正确的',因为你指出我正确的方向。谢谢:) – ATaylor
Thx,我相应地更新了答案。 –