QFileSystemModel::canFetchMore() 总是返回 false

Posted

技术标签:

【中文标题】QFileSystemModel::canFetchMore() 总是返回 false【英文标题】:QFileSystemModel::canFetchMore() returns false always 【发布时间】:2012-09-24 09:19:45 【问题描述】:

我有以下代码,我想在其中检查是否存在任何子目录。我希望 canFetchMore() fn 返回 true(因为根目录包含子目录)。但它返回错误。在调用 canFetchMore() fn 之前是否需要调用其他 fn。

QFileSystemModel model;
model.setFilter(QDir::AllDirs);
model.setRootPath("/");
QModelIndex index = model.index(model.rootPath());
qDebug()<<index.child(0,0).isValid()<<model.canFetchMore(index)<<index;

我尝试过使用 hasChildren() fn,无论文件夹是否包含子目录,它总是返回 true。

【问题讨论】:

【参考方案1】:

奇怪,没有这方面的文档。 方法

  model.setRootPath("/");

自动呼叫model.fetchMore(index);。这意味着此时所有子目录都已找到。这就是您调用 model.canFetchMore(index) 返回 false 的原因,因为没有更多子目录可供提取。

一种方法(使用QDir、QFileInfo):

QDir mytopdir("/path/to/dir");
if(mytopdir.exists())
    QFileInfoList list = mytopdir.entryInfoList();
    foreach(QFileInfo fileInfo, list)
        if(fileInfo.isDir())
            //FOUND IT!!
        
    

【讨论】:

谢谢。但是如何查找目录是否有子目录 ". and .." 将被 QDir 视为目录。为了阻止它,我们应该调用 QDir::setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);它也适用于空目录。

以上是关于QFileSystemModel::canFetchMore() 总是返回 false的主要内容,如果未能解决你的问题,请参考以下文章