How can I rescale an image and set it to QWidget?

7.3k views Asked by At
QPixmap pic("../image.jpg");

setAutoFillBackground(true);
QPalette palette;
QPixmap scaled=pic.scaled ( 800, 480, Qt::IgnoreAspectRatio, Qt::FastTransformation );

palette.setBrush(QPalette::Window, QBrush(scaled));

//this->setPalette(palette);
QWidget *w= new QWidget(this);
w->setGeometry(0,0,800,480);
w->show();
w->setPalette(palette); 

But the widget does not show any image.

2

There are 2 answers

2
Dusty Campbell On BEST ANSWER

Are you just trying to show the scaled image in a widget? I don't think setting the image in the brush and then setting the brush in the palette is the correct approach.

You can just use a QLabel to show an image in a widget. Like this:

QPixmap pic("../image.png");
QPixmap scaled=pic.scaled ( 800, 480, Qt::IgnoreAspectRatio, Qt::FastTransformation );

QLabel *label = new QLabel(this);
label->setPixmap(scaled);
0
Alex Prilutskiy On

You are showing the widget before setting the palette. Try setting the palette first.

If that does not help, try specifying the full file path.