PDA

View Full Version : Centering a scaled image



gpuckett
4th September 2014, 21:11
I have two questions.

First I am displaying a scaled image in a QLabel object on the Ui. Is it more efficient to scale the QImage before transforming into the QPixmap or is it more efficient to convert it into the QPixmap and then scaling it?

Second I am using the KeepAspectRationByExpanding option. This displays the image from the top, left which means the center of the image may potentially fall outside of the label. Is there an easy way to change it to display the center of the image at the physical center of the label and let the outsides of the image be truncated?

I'm trying to avoid getting into writing my own paint routine for this, I've never tried that and don't have much time to learn the basics of custom paint functions. Is there another way?

anda_skoa
4th September 2014, 22:03
First I am displaying a scaled image in a QLabel object on the Ui. Is it more efficient to scale the QImage before transforming into the QPixmap or is it more efficient to convert it into the QPixmap and then scaling it?

Might depend on the platform but my guess is that scaling the QImage is better.
I remember that at least at some point scaling a pixmap meant that it was transferred into a QImage internally, then scaled, then c



Second I am using the KeepAspectRationByExpanding option. This displays the image from the top, left which means the center of the image may potentially fall outside of the label. Is there an easy way to change it to display the center of the image at the physical center of the label and let the outsides of the image be truncated?


Create a QRect that has the size of the visible area.
Scale the image and get its rect(), take that rect's center() and move the first rectangle's center to that point.
Then use this modified rect on the scaled image's copy() and thus get the section you want to display.

Cheers,
_