qt C++ 在QWidget界面插入背景图不能完全展示怎么办
如果在QWidget界面插入背景图无法完全展示,可以尝试使用QLabel来显示背景图,并将其设置为QWidget的子控件。
首先,将QLabel添加为QWidget的子控件:
QLabel *backgroundLabel = new QLabel(this);
backgroundLabel->setObjectName("backgroundLabel");
backgroundLabel->setGeometry(0, 0, width(), height());
backgroundLabel->lower();
然后,使用QPixmap加载背景图片,并设置给QLabel:
QPixmap backgroundPixmap("path/to/background/image.png");
backgroundLabel->setPixmap(backgroundPixmap.scaled(backgroundLabel->size(), Qt::KeepAspectRatioByExpanding));
最后,在QWidget的样式表中设置QLabel的样式,使其充满整个QWidget:
QString styleSheet = "QLabel#backgroundLabel {"
" background-color: transparent;"
" border-image: url(path/to/background/image.png) 0 0 0 0 stretch stretch;"
"}";
setStyleSheet(styleSheet);
这样就可以将背景图完全展示在QWidget界面上了。
原文地址: https://www.cveoy.top/t/topic/ixQC 著作权归作者所有。请勿转载和采集!