tags:

views:

59

answers:

1

I have a Widget with QScrollArea in it and I want it to be scrolled down right after the widget containing it is shown. I tried:

scrollArea->ensureVisible(0,100, 20, 20);

It works only when invoked by user (pushing button for example). Putting it in widget contstructor or showEvent doesn't work. Can it be done automatically?

+1  A: 

I believe you can scroll the QScrollArea content by setting positions to its horizontal and vertical scrollbars. Smth, like this:

scrollArea->verticalScrollBar()->setValue(scrollArea->verticalScrollBar()->value() + 10);
scrollArea->horizontalScrollBar()->setValue(scrollArea->horizontalScrollBar()->value() + 10);  

code above should scroll contents of the scroll area 10 pixels down and 10 pixels right each time it gets called

hope this helps, regards

Edit0: extra code snippet showing how to scroll the area in the form's constructor:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QLabel *imageLabel = new QLabel;
    QImage image("my_large_image_file.JPG");
    imageLabel->setPixmap(QPixmap::fromImage(image));

    ui->scrollArea->setBackgroundRole(QPalette::Dark);
    ui->scrollArea->setWidget(imageLabel);

    ui->scrollArea->horizontalScrollBar()->setValue(100);
    ui->scrollArea->verticalScrollBar()->setValue(100);
}
serge_gubenko
Unfortunately, when invoked from widget constructor or showEvent, it doesn't work either.
majaen
I guess you should post up some of your code, as this solution works for me in my form's widget constructor
serge_gubenko
Ok, I found the bug. Before setting scrollbars there was resize(maximumSize()), which is obviously not a good idea (however I'm not sure why that influenced scrollAreas behaviour in that way).
majaen