Resizing QPixMap to fit QLabel issue
-
Re: Scaling QPixmap to fit Label
So I have been trying to do this multiple ways,
So far I have
label->setPixmap(pixmap); label->setScaledContents(true);
Now the above does scale the image so that it fits, however the problem is that the image looks all compressed and ugly. Is there maybe some better format other than QPixmap, or some way to fix the image so it looks good?
-
Re: Scaling QPixmap to fit Label
So I have been trying to do this multiple ways,
So far I have
label->setPixmap(pixmap); label->setScaledContents(true);
Now the above does scale the image so that it fits, however the problem is that the image looks all compressed and ugly. Is there maybe some better format other than QPixmap, or some way to fix the image so it looks good?
If you know the size of the label on beforehand, and if it is fixed, then you could simply use:
label->setPixmap(pixmap.scaled(myWidth, myHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
or with
Qt::IgnoreAspectRatio
.In any case, drop the
setScaledContents(true);
part.If your label size varies dynamically, e.g. as part of a layout when resizing the window, then you must subclass
QLabel
to implement theresizeEvent
handler, as done here:
https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio -
Re: Scaling QPixmap to fit Label
So I have been trying to do this multiple ways,
So far I have
label->setPixmap(pixmap); label->setScaledContents(true);
Now the above does scale the image so that it fits, however the problem is that the image looks all compressed and ugly. Is there maybe some better format other than QPixmap, or some way to fix the image so it looks good?
@Qtstarter121 said in Resizing QPixMap to fit QLabel issue:
label->setScaledContents(true);
instead of this, scale the pixmap to labels width and height
pixmap.scaled(label.width(),label.height());
-
If you know the size of the label on beforehand, and if it is fixed, then you could simply use:
label->setPixmap(pixmap.scaled(myWidth, myHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
or with
Qt::IgnoreAspectRatio
.In any case, drop the
setScaledContents(true);
part.If your label size varies dynamically, e.g. as part of a layout when resizing the window, then you must subclass
QLabel
to implement theresizeEvent
handler, as done here:
https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio@Diracsbracket The label size does vary dynamically. I set a scaled pixmap like you wrote and also included the same line in my event filter ( on resize event ) for my class but it didn't scale properly. I know for a fact that my label's height should have changed but the height of the pixmap didn't. However if I implement the same line with
Qt::IgnoreAspectRatio
Then it produces the same result I observed with just :
label->setPixmap(pixmap); label->setScaledContents(true);
And like I said, it's scaled, but looks ugly as it is way too compressed horizontally.
I'm thinking maybe this has something to do with the original size format of my pixmap and maybe it just won't scale the way I want it to unless I recreate it with a better size in mind - although not sure how that would work.
-
@Diracsbracket The label size does vary dynamically. I set a scaled pixmap like you wrote and also included the same line in my event filter ( on resize event ) for my class but it didn't scale properly. I know for a fact that my label's height should have changed but the height of the pixmap didn't. However if I implement the same line with
Qt::IgnoreAspectRatio
Then it produces the same result I observed with just :
label->setPixmap(pixmap); label->setScaledContents(true);
And like I said, it's scaled, but looks ugly as it is way too compressed horizontally.
I'm thinking maybe this has something to do with the original size format of my pixmap and maybe it just won't scale the way I want it to unless I recreate it with a better size in mind - although not sure how that would work.
@Qtstarter121 said in Resizing QPixMap to fit QLabel issue:
I'm thinking maybe this has something to do with the original size format of my pixmap
What is the unscaled pixel size of your pixmap?
Can you post a minimal runnable code version of how you are currently doing things? -
Re: Scaling QPixmap to fit Label
So I have been trying to do this multiple ways,
So far I have
label->setPixmap(pixmap); label->setScaledContents(true);
Now the above does scale the image so that it fits, however the problem is that the image looks all compressed and ugly. Is there maybe some better format other than QPixmap, or some way to fix the image so it looks good?
@Qtstarter121 the setScaledContents(True) worked perfectly for me on python. thanks.