chai
chai

Reputation: 1483

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.

  1. I want the image to scale to the size of the QLabel.
  2. I do not want the image repeating it self when the size of the QLabel goes beyond the size the image.

Please help.

Upvotes: 2

Views: 15071

Answers (3)

Thibault T
Thibault T

Reputation: 490

Try to use the QLabel function

setScaledContents(true);

Upvotes: 2

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

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.

Upvotes: -1

Liz
Liz

Reputation: 8958

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);

Upvotes: 3

Related Questions