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

Luis Garrido luisgarrido at users.sourceforge.net
Thu Aug 13 02:45:53 CEST 2009


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())
		mFSView->scrollTo(currentPathIndex, QAbstractItemView::PositionAtTop);
}

void MainWindow::showEvent(QShowEvent *event) {
	firstShow();
	QMainWindow::showEvent(event);
}

This code works fine _after_ the application has started, when I
minimize and restore the window, for instance, but it won't the _first
time_ the window is made visible: the tree is expanded, but the widget
won't scroll and will show the top of the tree instead of the desired
folder.

I have tried some ugly QTimer::singleShot hackery substituting
firstShow(); above for:

	QTimer::singleShot(0, this, SLOT(firstShow()));

In this case the widget scrolls to a place in-between the top of the
tree and the desired folder. That place is a bit further down the tree
if the timeout is longer than 20ms, but it won't go any further.

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

I am using Qt 4.5.2.

Thanks in advance,

Luis



More information about the Qt-interest-old mailing list