c++qtmeego

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


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.


Solution

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