1

I have the following code in which i want to check if there are any sub directories exist. I expect the canFetchMore() fn to return true(since root directory contains sub directories). But it returns false. Is there any other fn to be called before calling canFetchMore() 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;

I have tried using hasChildren() fn and it returns always true irrespective of the folder contains sub-dirs or not.

4

1 回答 1

1

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

  model.setRootPath("/");

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

一种方法(使用QDirQFileInfo):

QDir mytopdir("/path/to/dir");
if(mytopdir.exists()){
    QFileInfoList list = mytopdir.entryInfoList();
    foreach(QFileInfo fileInfo, list){
        if(fileInfo.isDir()){
            //FOUND IT!!
        }
    }
}
于 2012-09-24T09:54:53.760 回答