PDA

View Full Version : How can I always keep the scroll bar at the bottom of a QScrollArea?



Plixil
28th July 2010, 16:44
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:


scrollArea->SetBarLocation(ScrollArea::BOTTOM);

or something along those lines? Thanks :)

franz
29th July 2010, 05:52
A very brief browse through the docs yields:
http://doc.trolltech.com/latest/qabstractscrollarea.html#horizontalScrollBarPolicy-prop
http://doc.trolltech.com/latest/qabstractscrollarea.html#verticalScrollBarPolicy-prop

ChrisW67
29th July 2010, 07:02
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.

Plixil
31st July 2010, 00:41
I found two ways:



//method one
m_ScrollArea->verticalScrollBar()->setSliderPosition(m_ScrollArea->verticalScrollBar()->maximum());

//method two
m_ScrollArea->ensureVisible(0, scrollAreaHeight, 0, 0);


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?

norobro
31st July 2010, 01:13
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.

Plixil
31st July 2010, 02:27
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.

norobro
31st July 2010, 02:41
You need to reset the maximum. Try the attached app.

ChrisW67
31st July 2010, 05:15
Try connecting rangeChanged() from the scroll bar to a a slot:


void sliderRangeChanged(int min, int max) {
Q_UNUSED(min);
QScrollBar *bar = scroll->verticalScrollBar();
bar->setValue(max);
}

Plixil
31st July 2010, 13:24
I was blown away by the amount of help I got here. Thanks guys :D For future Googlers, here's there code I ended up using.


QScrollBar* scrollbar = scrollArea->verticalScrollBar();
QObject::connect(scrollbar, SIGNAL(rangeChanged(int,int)), this, SLOT(moveScrollBarToBottom(int, int)));




void moveScrollBarToBottom(int min, int max)
{
Q_UNUSED(min);
scrollArea->verticalScrollBar()->setValue(max);
}


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

Thanks again guys :D

norobro
31st July 2010, 19:21
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. :o

I think that your solution is the correct one.

youssef attai
2nd August 2021, 21:34
I was blown away by the amount of help I got here. Thanks guys :D For future Googlers, here's there code I ended up using.


QScrollBar* scrollbar = scrollArea->verticalScrollBar();
QObject::connect(scrollbar, SIGNAL(rangeChanged(int,int)), this, SLOT(moveScrollBarToBottom(int, int)));




void moveScrollBarToBottom(int min, int max)
{
Q_UNUSED(min);
scrollArea->verticalScrollBar()->setValue(max);
}


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

Thanks again guys :D

Thank you so much it finally worked for me

jhosek
4th August 2021, 10:53
From QT source for QAbstractItemView:

void QAbstractItemView::scrollToBottom()
{
Q_D(QAbstractItemView);
if (d->delayedPendingLayout) {
d->executePostedLayout();
updateGeometries();
}
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}

CEO21
4th August 2021, 15:47
Hello, please how can I update a new post?
I am new in this platform and have a question to ask.

d_stranz
4th August 2021, 16:47
Go to the Newbie section of the forum (https://www.qtcentre.org/forums/4-Newbie) and click the "Post New Thread" button at the top of the page. Do not hijack an existing post like you've just done here.