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.
المحلول
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);
نصائح أخرى
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.
لا تنتمي إلى StackOverflow