QFileSystemModel rowCount not updated until 2nd click?
-
I'm new to Qt and seek for help. Everything so far works just fine but...
void MainWindow::on_treeView1_clicked(const QModelIndex &index) { QString sPath = dirmodel->fileInfo(index).absoluteFilePath(); ui->listView1->setRootIndex(filemodel->setRootPath(sPath)); ui->treeView1->expand(dirmodel->setRootPath(sPath)); QModelIndex parentIndex = ui->listView1->rootIndex(); int numRows = filemodel->rowCount(parentIndex); for (int row = 0; row < numRows; ++row) { QModelIndex index2 = filemodel->index(row, 0, parentIndex); QString teXt = filemodel->data(index2, Qt::DisplayRole).toString(); qDebug() << teXt; } qDebug() << numRows; }
... numRows does not update until the second click on a path. On the first click listView1 gets updated with the files in the current path but numRows are still 0. If i click a 2nd time on the link or come back from another path, numRow is correct.
Any ideas?
Thanks!
-
Hi, welcome to Devnet.
I'm not sure but QFileSystemModel is using lazy loading to display only unfolded directories. The reason is simple: imagine if a user sets the model root to their computer's root, it would consume a lot of resources to display their whole computer hierarchy. Therefore, QFileSystemModel is threaded and run asynchronously. This enables to prevent your interface from freezing if there are a lot to display when the user unfolds a directory.
With that in mind, I would say that numRows is not updated when you think it would be. But it's only a guess, I can't explain why it's done correctly the second time. I'm not even sure that this behavior is the same on every computer, with any root directory.
-
Hi, welcome to Devnet.
I'm not sure but QFileSystemModel is using lazy loading to display only unfolded directories. The reason is simple: imagine if a user sets the model root to their computer's root, it would consume a lot of resources to display their whole computer hierarchy. Therefore, QFileSystemModel is threaded and run asynchronously. This enables to prevent your interface from freezing if there are a lot to display when the user unfolds a directory.
With that in mind, I would say that numRows is not updated when you think it would be. But it's only a guess, I can't explain why it's done correctly the second time. I'm not even sure that this behavior is the same on every computer, with any root directory.
I was curious and did set a wait 1000ms after
ui->treeView1->expand(dirmodel->setRootPath(sPath));
and the result was a freezing app. Nothing was updated until the second was over. Well, OK, if all the files are loaded in listView1 after treeView1 was clicked the reading from the file system should be done. So i did try to find a way / slot like "on_listView1_is_updated" but no luck so far.
Or is there a better way to loop through files after QFileSystemModel is done?
-
-
Not sure if you solved this or not but QFileSystemModel emits the directoryLoaded signal once the background thread has completed loading the directory. Would that work for you?
-
Not sure if you solved this or not but QFileSystemModel emits the directoryLoaded signal once the background thread has completed loading the directory. Would that work for you?
@rturrentine
Excellent!@qDebug
Just connect this signal to a slot that updates the numRows and it should work just fine.