Results 1 to 10 of 10

Thread: How can I always keep the scroll bar at the bottom of a QScrollArea?

  1. #1
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How can I always keep the scroll bar at the bottom of a QScrollArea?

    In MSN messenger or GTalk, we don't have to scroll down to see the newest messages because the scroll bar is always at the bottom of the window with the message. How can I achieve the same effect in my program? Is there a function like:
    Qt Code:
    1. scrollArea->SetBarLocation(ScrollArea::BOTTOM);
    To copy to clipboard, switch view to plain text mode 
    or something along those lines? Thanks

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    QAbstractSlider::setValue() and QAbstractSlider::maximum() should go together nicely to put the vertical scroll bar at the bottom of the slider. Now you have to work out when this should be done.

  4. The following user says thank you to ChrisW67 for this useful post:

    Plixil (30th July 2010)

  5. #4
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    I found two ways:

    Qt Code:
    1. //method one
    2. m_ScrollArea->verticalScrollBar()->setSliderPosition(m_ScrollArea->verticalScrollBar()->maximum());
    3.  
    4. //method two
    5. m_ScrollArea->ensureVisible(0, scrollAreaHeight, 0, 0);
    To copy to clipboard, switch view to plain text mode 

    This code works but it only brings me to the position that was the lowest before the last widget was added. I don't know why this is happening as I run this code AFTER the new widget is added. Any ideas?

  6. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    From ChrisW67's post:
    QAbstractSlider::setValue() and QAbstractSlider::maximum() should go together
    Note the "go together". If you reset the maximum you don't change the position of the slider.
    Last edited by norobro; 31st July 2010 at 01:14. Reason: Can't type

  7. The following user says thank you to norobro for this useful post:

    Plixil (31st July 2010)

  8. #6
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    I'm not resetting the maximum. I'm just using it to get the maximum position (but the function call name is a bit weird so I understand the confusion).

    I'll very happily settle for a hack here. Is there any way of knowing whether or not the scroll area has reached a point where the scroll bar is needed and becomes active? I can't find such a function in the docs.

  9. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    You need to reset the maximum. Try the attached app.
    Attached Files Attached Files

  10. The following user says thank you to norobro for this useful post:

    Plixil (31st July 2010)

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    Try connecting rangeChanged() from the scroll bar to a a slot:
    Qt Code:
    1. void sliderRangeChanged(int min, int max) {
    2. Q_UNUSED(min);
    3. QScrollBar *bar = scroll->verticalScrollBar();
    4. bar->setValue(max);
    5. }
    To copy to clipboard, switch view to plain text mode 

  12. The following 2 users say thank you to ChrisW67 for this useful post:

    norobro (31st July 2010), Plixil (31st July 2010)

  13. #9
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    I was blown away by the amount of help I got here. Thanks guys For future Googlers, here's there code I ended up using.
    Qt Code:
    1. QScrollBar* scrollbar = scrollArea->verticalScrollBar();
    2. QObject::connect(scrollbar, SIGNAL(rangeChanged(int,int)), this, SLOT(moveScrollBarToBottom(int, int)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void moveScrollBarToBottom(int min, int max)
    2. {
    3. Q_UNUSED(min);
    4. scrollArea->verticalScrollBar()->setValue(max);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Though I used Chris's method, norobro's was equally valid.

    Thanks again guys

  14. The following user says thank you to Plixil for this useful post:

    norobro (31st July 2010)

  15. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How can I always keep the scroll bar at the bottom of a QScrollArea?

    I encountered this problem several months ago. I tried several different solutions before arriving at "maximum()+1" which IMO is a kludge. I even tried calling update() on my view to insure that scroll bar value was current. Somehow I overlooked the rangeChanged(int,int) signal.

    I think that your solution is the correct one.

Similar Threads

  1. QScrollArea and scroll
    By uKCuH in forum Newbie
    Replies: 1
    Last Post: 1st July 2010, 12:22
  2. Scroll Bar not appearing in QScrollArea
    By coolkaps in forum Newbie
    Replies: 1
    Last Post: 2nd May 2010, 16:12
  3. Force a Scroll in a QScrollArea
    By nmuntz in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2009, 13:49
  4. Auto scroll down in QScrollArea
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2008, 12:51
  5. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27

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.