[Qt-interest] QTreeView::scrollTo at application start

Stephen Kelly steveire at gmail.com
Thu Aug 13 07:56:12 CEST 2009


Luis Garrido wrote:

> I have an application that includes in its QMainWindow a QTreeView
> displaying a QFileSystemModel. I am trying to save the last folder the
> user was working with when she closes the application so I can display
> it back when she runs it again.
> 
> void MainWindow::firstShow() {
> QModelIndex currentPathIndex = mFSModel->index(lastSessionPath);
> if(currentPathIndex.isValid())

I'm guessing the currentPathIndex is not valid. FSM is populated dynamically 
AFAIK, which means that the row you want to scroll to is not in the model 
yet when the application starts.

> Can anyone suggest a more reliable method to achieve this result, please?

Watch for the rowsInserted Signal from the model, and if it matches the 
lastSessionPath, then scrollTo it. Note that you'd probably want to 
disconnect from the rowsInserted signal right afterwards. See the 
ModelWatcher class at the top of this for one way of doing something 
similar.

http://websvn.kde.org/trunk/KDE/kdelibs/kdeui/tests/kselectionproxymodeltest-
selectedbranches.cpp?view=markup

> 
> I am using Qt 4.5.2.
> 
> Thanks in advance,

Let us know if that works.

Steve

> 
> Luis




More information about the Qt-interest-old mailing list