Results 1 to 3 of 3

Thread: Problem with scrolling to the bottom of a QListView

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Unhappy Problem with scrolling to the bottom of a QListView

    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

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with scrolling to the bottom of a QListView

    i would try to connect to the QScrollBar's rangeChanged ( int min, int max ) signal

  3. #3
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: Problem with scrolling to the bottom of a QListView

    Thank you MrDeath, very useful suggestion.

    This is going to work. It turns out that the rangeChanged signal can be emitted several times over as the view is built by the Qt framework.

    So in my slot I will attempt to scroll to the position I want to be at, then check whether I actually managed to scroll there: if yes, then disconnect from the signal, if no, stay connected and try again next time it is emitted.

Similar Threads

  1. Scrolling to bottom of Qt 4 QTextEdit
    By DiamonDogX in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2010, 16:31
  2. QTableWidget - scrolling to bottom of the table
    By mkhoang in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2008, 17:21
  3. Replies: 7
    Last Post: 15th November 2007, 17:19
  4. Scrolling bar problem in QScrollArea
    By YuriyRusinov in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 21:22
  5. Keeping focus at bottom of QListView
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2006, 14:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.