PDA

View Full Version : Force a Scroll in a QScrollArea



nmuntz
25th January 2009, 04:17
Hi,

I have a QScrollArea with some widgets inside and I would like to force it to scroll all the way to the right when I click a button after a new widget is created inside this scrollarea.

This is what I have so far:
The QScrollArea is called imageList:



int max = imageList->horizontalScrollBar()->maximum();
imageList->horizontalScrollBar()->setValue(max);


However it is not scrolling all the way to the right. It is scrolling to the element before the last one. Obviously this code is placed after adding the last widget to the QScrollArea.
Am I doing something wrong?

Thanks a lot in advance for your help.

seneca
25th January 2009, 09:36
I think at the time your code is executed the scroll area has not yet updated the scrollbar size. Try executing the code after all events are processed, by putting the code into a private slot and trigger it by a single shot timer:



...
QTimer::singleShot(0, this, SLOT(scrollRight()));
...

void MyClass::scrollRight()
{
int max = imageList->horizontalScrollBar()->maximum();
imageList->horizontalScrollBar()->setValue(max);
}

nmuntz
25th January 2009, 14:49
That worked perfectly !
Thanks a lot for your help!