Results 1 to 8 of 8

Thread: ListView in a ScrollView: How to know scroll position (is it at bottom?)

  1. #1
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Hi,

    I have a WML application with a ScrollView and a ListView inside. The items from the ListView has variable height.

    I need to know when the scroll bar is moved, in fact when it is at bottom and when it is NOT.

    My target is to keep scroll at bottom (positionViewAtEnd()) when I add an item to the ListView ONLY if the scroll is at bottom. If NOT at bottom, positionViewAtEnd() will not be used.

    I have tried "playing" with height, contentHeigh & contentY. Sometimes it works (when scroll is at bottom: contentHeight == contentY + height), but other times contentY value changes to negative values, and my code fails...

    Any help?


    Thanks a lot

    Diego
    Last edited by ddonate; 23rd June 2016 at 10:11.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    A bit of code would help to understand what your situation actually looks like.

    Do you use the ScrollView as a decorator on the ListView, i.e. as a means to provide a scrollbar for the ListView?
    Or is the ListView on of the items inside the ScrollView's content item?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Thanks for your response.

    I didn't add code since I am afraid I don't know what to add. I have just a ScrollView to provide a scrollbar for the ListView, as you said. I dind't specify that, sorry

    The delegate displays incoming text in a kind of "bubbles", like a chat. Since the text is not fixed, the height of the ListView items (text messages) is variable.

    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Ok, so when the ListView is scrolled to the end, you want it to stay at the end, but if it is not, it should not move even if items are appended.
    Does that sound right?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Exactly!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Have you tried this with just the ListView alone?

    How do you trigger your scrolling code?

    What kind of model are you using?

    Code would really help

    Cheers,
    _

  7. #7
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    Hi,

    I have tried with 'atYEnd' in several property changes, to detect if scroll is at bottom or not

    It seems to work, and then I use that in 'onCountChanged' to put (or not) the scroll at the bottom

    That works once all the ListView height is full of messages, but NOT in ONE case: when the incoming message is the one that fills the
    ListView height (1st time that contentY is NOT '0').

    I don't know if it is clear...

    I have simplified my code to test (including the delegate), and now it seems like that:

    Qt Code:
    1. FocusScope {
    2. clip: true
    3.  
    4. id: focusScopeView
    5.  
    6. width: parent.width; height: parent.height
    7.  
    8. ScrollView {
    9.  
    10. width: parent.width; height: parent.height
    11.  
    12. ListView {
    13. id: listTexts
    14.  
    15. width: parent.width; height: parent.height
    16.  
    17. property bool bScrolled: false
    18.  
    19. model: textsModel
    20. delegate: Text { text: "Text:\t" + eventText }
    21.  
    22. onCountChanged: {
    23.  
    24. if (!bScrolled)
    25. positionViewAtEnd();
    26. }
    27.  
    28. onContentYChanged: {
    29. bScrolled = !atYEnd;
    30.  
    31. if (atYEnd)
    32. positionViewAtEnd()
    33. }
    34.  
    35. onContentHeightChanged: {
    36. if (!bScrolled)
    37. positionViewAtEnd();
    38. }
    39. }
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    Thanks and regards!

    Diego
    Last edited by ddonate; 24th June 2016 at 09:40.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ListView in a ScrollView: How to know scroll position (is it at bottom?)

    What you could try is this:

    - have a property for "old content height"
    - when content height grows, check if the old value is less than the visible height and the new value is greater than or equal, then you have filled the view.

    Alternatively you could connect to the model's signals that indicate beginning of row changes (QAbstractItemModel::rowsAboutToBeInserted(), QAbstractItemModel::rowsAboutToBeRemoved()) and evaluate the situation for bScrolled there:
    if content height is less than or equal the visible height or yAtEnd then scrolled is false

    Cheers,
    _

Similar Threads

  1. Replies: 13
    Last Post: 4th August 2021, 17:47
  2. Replies: 0
    Last Post: 7th February 2015, 17:01
  3. QPlainTextEdit scroll to bottom
    By vrltwe in forum Newbie
    Replies: 1
    Last Post: 15th August 2014, 22:44
  4. Replies: 0
    Last Post: 30th January 2014, 14:35
  5. Replies: 1
    Last Post: 29th September 2011, 08:51

Tags for this Thread

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.