How to resize an image inside QLabel
I am developing a custom plugin in Qt and there is this situation where I have to build a widget which has some an Image on it. So I am using QLabel as base class for my custom widget. Here's the code for paint event
QPixmap pic("/general开发者_运维技巧/source/pic.png");
setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::Window, QBrush(pic));
this->setPalette(palette);
Now the image is rendered on the QLabel, but this is not what I desired.
- I want the image to scale to the size of the QLabel.
- I do not want the image repeating it self when the size of the QLabel goes beyond the size the image.
Please help.
Assuming you can get the size of your control you can scale your pixmap before you set it in the brush using
pic.scaled ( width, height, Qt::IgnoreAspectRatio, Qt::FastTransformation )
This returns another QPixmap which you can pass to your QBrush.
Just for reference, you can also use a style sheet to set the border image for your control.
border-image: url( yourImage);
Try to use the QLabel function
setScaledContents(true);
If you have a custom widget class, you could override the paintEvent and do the proper drawing at that point. I don't know if you could just draw the pixmap scaled to the proper size and call the parent's class to finish the drawing, or if you'd have to do all of it yourself.
精彩评论