PDA

View Full Version : Dynamically scale QLabel with QImage to content while keeping aspect ratio



Excludos
2nd September 2016, 12:33
I have a QImage that I want to show in the application (easiest way seems to be through a QLabel). However the application is meant to be able to use on different screen sizes, which means I can't just keep a rigid image size and call it a day. Using setScaledContent(true) is almost perfectly what I'm looking for, but unfortunately it doesn't care about aspect ratio.

Any ideas?

edit: Should probably have mentioned. I'm using QT 5.5.1 on Windows

anda_skoa
2nd September 2016, 13:12
You can scale the image before setting it on the label, usnig the label's size as the target size.

QImage::scaled() has flags for things like keeping the aspect ratio

Cheers,
_

Excludos
2nd September 2016, 13:25
That's only if you already know what size it needs to be. I figured if you setSizePolicy to MinimumExpanding in both directions, you can save the qlabel's width and height as int and use that in the QImage::scaled() method. That still requires you to set all your layouts correctly of course, or both widgets will just use half the layout each.

But thanks for the answer! :)

^NyAw^
2nd September 2016, 13:44
Hi,

You can catch the "resizeEvent" of you main window(or your QLabel) and there you can resize the QImage to fit the new QLabel size. Remeber to keep the original QImage