QFileSystemModel::rowCount return 0
-
QFileSystemModel *model = new QFileSystemModel; QString initPath("/"); model->setRootPath(initPath); QModelIndex parentIndex = model->index(initPath); bool ok = parentIndex.isValid(); if (ok) { int numRows = model->rowCount(parentIndex); // numRows == 0, why? int numCols = model->columnCount(parentIndex); // numCols == 4, as expected }
-
You have to wait for QFileSystemModel::directoryLoaded()
-
@Christian-Ehrlicher said in QFileSystemModel::rowCount return 0:
You have to wait for QFileSystemModel::directoyLoaded()
OK, however in the doc Model/View Programming:
QFileSystemModel *model = new QFileSystemModel; QModelIndex parentIndex = model->index(QDir::currentPath()); int numRows = model->rowCount(parentIndex);
PS: to locate the code, search
numRows
, the first one. -
I'll take a look at the docs later this day to see if it's correct.
-
@jronald , @Christian-Ehrlicher
You are right, that example (at link https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes) is not accurate forQFileSystemModel
behaviour, it's intended to illustrate the principle of the code it's showing but has picked an unsuitable model.From https://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance:
Unlike
QDirModel
,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 torowCount()
will return 0 until the model populates a directory.Perhaps therefore the docs example could just substitute
QDirModel
forQFileSystemModel
in that light? -
Documentation update is in progress.