QLabel::setPixmap() and QScrollArea::setWidget()

2019-05-21 02:48发布

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?

1条回答
Luminary・发光体
2楼-- · 2019-05-21 03:42

Set your scroll area's widgetResizable property to true:

scrollArea->setWidgetResizable(true);
查看更多
登录 后发表回答