从QFilesystemmodel查找(搜索)特定命名文件
问题描述:
我是Qt中的新手。我在QTreeview中使用QFileSystemModel来研究驱动器的内容。我想从使用这个QFileSystemModel的任何驱动器中找到一个特定的文件。现在,有没有办法从这个模型中找到这个特定的文件? 在此先感谢。从QFilesystemmodel查找(搜索)特定命名文件
答
QFileSystemModel
不提供任何搜索方法。您应该改用QDirIterator。
QString find_file(QString dir, QString name) {
QDirIterator it(dir, QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
if (it.fileName() == name) {
return it.filePath();
}
}
return QString();
}
您可以使用QFileSystemModel::index
方法将文件路径转换为模型索引。