Programmatically scroll QScrollArea

I have spent some time with the debugger and find out that scriollArea has 0 sizes in the constructor, so it looks like it is possible to scroll something only when all the widgets are created and visible. Scrolling in the showEvent of window works fine.


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);
}

Tags:

Qt

Qt4