Question

I've been tracking down a bug that boils down to this - if you show an image label inside a scroll area, the label will not be resized to the image's size if QLabel::setPixmap() is called after QScrollArea::setWidget().

This example illustrates the problem, just replace /path/to/some/image.png with some real image on your computer:

QScrollArea *scrollArea = new QScrollArea;
QLabel *label = new QLabel(scrollArea);
scrollArea->setWidget(label);
label->setPixmap(QPixmap("/path/to/some/image.png"));
scrollArea->show();

If you swap the lines to call setPixmap() before setWidget(), the label will be properly resized.

Why does this happen, and how can I force the label to resize properly?

Was it helpful?

Solution

Set your scroll area's widgetResizable property to true:

scrollArea->setWidgetResizable(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top