View Full Version : QFileSystemModel - Incremental update/pre-emptive update

2nd September 2011, 14:56
From the Qt documentation:

QFileSystemModel will not fetch any files or directories until setRootPath() is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows. Unlike QDirModel(obsolete), QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory. QFileSystemModel keeps a cache with file information. The cache is automatically kept up to date using the QFileSystemWatcher.

I'm using QTreeView together with a sub-classed QFileSystemModel which uses checkable boxes.
If I call QFileSystemModel::rowCount(index) before an item has been expanded in the tree I will receive '0', regardless of whether there are any subdirectories or files. However, once it has been expanded the correct row count will then be given when called again.

I think if you call QFileSystemModel::setRootPath() this will fetch the data from the specified file path, but it seems that it does not 'execute fast enough' (the cache is not updated) before I call QFileSystemModel::rowCount in my code below.

// Whenever a checkbox in the TreeView is clicked
bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
if (role == Qt::CheckStateRole)
if (value == Qt::Checked)
emit dataChanged(index, index);
return true;

return QFileSystemModel::setData(index, value, role);

// Counts how many items/children the node has (i.e. file/folders)
void MyModel::set_children(const QModelIndex& index)
int row = this->rowCount(index);

qDebug() << QString::number(row);


Is there a way I can pre-emptively gather the sub-folder information before I try to count how many items are contained in that folder?