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

送分小仙女□ 提交于 2019-12-10 11:28:55

问题


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:


Set your scroll area's widgetResizable property to true:

scrollArea->setWidgetResizable(true);


来源:https://stackoverflow.com/questions/22653233/qlabelsetpixmap-and-qscrollareasetwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!