0
votes

I have a window with a Layout and a QLabel. The QLabel contains an image. The image should fill the QLabel's parent layout. Should the user decide to scale the window down, the image should shrink until it reaches a minimum size- at which point the window should no longer be scalable. Should the user decide to scale the window up, the image should grow. In both these cases, the aspect ratio of the image should be preserved.

I have managed to fill the QLabel's parent with the image using setScaledContents. However, the minimum size always seems to be the original size of the image file, nothing I define - when I do attempt to define it via QPixmap.scaled(), it makes the image extremely pixelated. And though I can scale the image up, the aspect ratio is never preserved.

I feel as though this should be fairly straightforward, but I am very lost. Help!

1

1 Answers

2
votes

Use setMinimumSize and setSizePolicy with a size policy of minimum, this should block the label from shrinking below the minimum size that you set.

Preserving the aspect ratio is a little more complicated. Fist save the pixmap of the image you want to draw as a member variable for the QLabel. Next re-implement the paint event for the label so that you can do the scaling of the pixmap yourself. If you call this->size() in the paint event you will get the area where the pixmap is to be drawn. Call scaled on your pixmap with that size and Qt::KeepAspectRatio, then setPixmap on your label. Finally call QLabel::paint_event.