如何重新缩放图像并将其设置为 QWidget

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

本文关键字:设置 QWidget 何重新 缩放 图像      更新时间:2023-10-16
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); 

但是小部件不显示任何图像。

您只是想在小部件中显示缩放的图像吗? 我不认为在画笔中设置图像然后在调色板中设置画笔是正确的方法。

您可以使用QLabel在小部件中显示图像。 喜欢这个:

QPixmap pic("../image.png");
QPixmap scaled=pic.scaled ( 800, 480, Qt::IgnoreAspectRatio, Qt::FastTransformation );
QLabel *label = new QLabel(this);
label->setPixmap(scaled);

您在设置调色板之前显示小部件。首先尝试设置调色板。

如果这没有帮助,请尝试指定完整的文件路径。