Hi all,

Our Symbian Qt4.6.3 app has a view based on a QListView using a QStandardItemModel.

When the data we are displaying is updated, we are resetting and repopulating the model, after which I want to automatically set the scroll position to the bottom of the list.

We have tried using scrollToBottom() and also verticalScrollBar()->setValue(). The trouble is, this doesn't scroll to the bottom if there is a lot of data, only a certain way down the view. Debugging this, I can see that the maximum() value of the scroll bar is limiting the extent to which scrolling is permitted.

It seems that the scroll bar extents are not set by the Qt framework immediately when I populate the model, but that this happens asynchronously some time after. Using a timer to introduce a small delay before calling verticalScrollBar()->setValue() has been found to work, but is a clumsly and proabably unreliable solution.

The best I've come up with is something like this:

Qt Code:
  1. void MyView::ifDataChanged( )
  2. {
  3. int savedScrollPosition = m_listView->verticalScrollBar()->value();
  4. populateModel(); // calls clear() and then a number of appendRow() calls on model
  5. while (m_listView->verticalScrollBar()->value() != savedScrollPosition)
  6. {
  7. QApplication::processEvents(); // need to allow the list view to do its stuff
  8. // before we can set the scroll position
  9. m_listView->verticalScrollBar()->setValue(savedScrollPosition);
  10. }
  11. }
To copy to clipboard, switch view to plain text mode 

Here I'm trying to scroll back to a saved scroll position rather than to the bottom of the view. Obviously I'll have problems if the saved scroll position is too big for the new size of the view. And this loop could lock up the CPU 100% and would be unacceptable in production code. But it works as a proof of concept and does scroll to the desired position.

My question is: is there a better way of doing this? Ideally I'd like to find some kind of signal emitted by the QListView when it has finished the task of working out its layout and setting the scroll ranges, following the repopulation of the model.

Hopefully this is enough information for someone to be able to make helpful suggestions - otherwise my colleague will prepare a stripped down demo app to demonstrate the problem.

Thanks in advance, John