1.在要换背景的类的构造函数中装载一个图片,变量要为全局的,接下来会用到
backgroundImage.load(":image/bg.png");
setAutoFillBackground(true);
2.实现resizeEvent函数,在里面画背景
void Example::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
QPalette pal(palette());
pal.setBrush(QPalette::Window,QBrush(backgroundImage.scaled(event->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
setPalette(pal);
}