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.
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.