ScrollTo file in QTreeView with QFileSystemModel on startup

I've been reading for the last few hours but haven't found a good solution to what would seem like a simple common problem. I have a QTreeView with QFileSystemModel. I want to set the current index to the last file saved by the user and scroll to that position. Since the qfilesystemmodel is loaded asynchronously, if I immediately use the scrollTo (mydesiredindex) function like:

Model = new QFileSystemModel;
Model->setRootPath(RootDirectory);
Model->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
ui.RootView->setModel(Model);
ui.RootView->setCurrentIndex(Model->index(LastUsedPath));
ui.RootView->scrollTo(Model->index(LastUsedPath));

      

qtreeview scrolls to the current file location, but then adds more files to it, so mydesiredindex is pushed out of view.

I tried to get a signal that the model has finished filling in the tree structure to no avail. directoryLoaded (const QString &) and rowsInserted (const QModelIndex &, int, int)) signals send signals before the model finishes filling.

Thanks for the help.

+3


source to share


1 answer


I believe it might have something to do with the ordering of your commands. I order it as follows

self.tree.scrollTo(index)
self.tree.expand(index)
self.tree.setCurrentIndex(index)

      

Or in your code



ui.RootView->scrollTo(Model->index(LastUsedPath));
ui.RootView->expand(Model->index(LastUsedPath));
ui.RootView->setCurrentIndex(Model->index(LastUsedPath));

      

Hope it helps.

+1


source







All Articles