PDA

View Full Version : Help with QFileSystemModel



TheShow
9th December 2009, 22:44
How can QFileSystemModel be used to create a QStringList of full pathnames of directories?

This is what I'm trying to do:


QStringList Info::getDirectoryTree(const QString & path)
{
QStringList paths;

QModelIndex parent = fileSystemModel->index(path);
int rowCount = fileSystemModel->rowCount(parent);
int columnCount = fileSystemModel->columnCount(parent);
for (int i=0; i<rowCount; i++)
{
for (int j=0; j<columnCount; j++)
{
QModelIndex mi = fileSystemModel->index(i, j, parent);
QFileInfo fileInfo = fileSystemModel->fileInfo(mi);
if (!paths.contains(fileInfo.absoluteFilePath()))
{
paths << fileInfo.absoluteFilePath();
}
bool hasChildren = fileSystemModel->hasChildren(mi);
if (hasChildren)
{
paths << getDirectoryTree(fileInfo.absoluteFilePath());
}
}
}

return paths;
}

If you set the root path on fileSystemModel, then call getDirectoryTree() above, it works fine for the first directory level. However, when it recursively calls getDirectoryTree() using the child nodes, the rowCount is always 0.

Obviously I'm just not understanding the concept behind QAbstractItemModel in order to iterate through all values. In my case there is a root directory with about 100 subdirectories, most of which have several subdirectories, etc. (over 3,500 total).

All I want to do is get all the paths of the directories in a QFileSystemModel.

squidge
9th December 2009, 22:53
QFileSystemModel can return zero for rowCount until the node is populated, as it uses a seperate thread for populating the tree. It'll send a signal when the count has changed.

I'm not really sure how to get it to what you want, and it's not really meant for that.

Maybe QDir would be better for you, as that isn't threaded, and you can call entryInfoList to get a list of all files and directories in the directory.

TheShow
10th December 2009, 13:28
Maybe that's the problem. I've already implemented the feature a few different ways, using QDirModel, QDir, and QDirIterator. The issue is with performance. There are almost 4,000 directories and it takes 3-4 seconds to perform the operation. I was just hoping for a faster solution.

squidge
10th December 2009, 17:28
What is the intended last result? As in, what are you going to do with your list of filenames once you have them?

There might be better way.

TheShow
5th January 2010, 20:11
The intended use is to find all directories below a starting directory that contain a certain string. That list will be presented at various places within the application and the search string may be different.