Qt Creator,如何设置图像作为背景图片
问题描述:
我只想将我的qt创作者窗口的背景设置为我保存在计算机上的图片。我试过右键单击背景,进入Change styleSheet并单击添加资源,但没有出现,我无法在计算机上搜索该文件。Qt Creator,如何设置图像作为背景图片
答
将文件添加到您的resources.qrc文件,该文件位于项目的源文件夹,在某种程度上,如:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>your_image.png</file>
</qresource>
</RCC>
后,在窗口中/小部件构造,加载图像像素映射所以它的存储用于其他用途(如设置背景)
QPixmap pixmap;
pixmap.load(":/your_image.png")
//scaling the image, optional. See the documentation for more options
pixmap = pixmap.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QPalette palette;
palette.setBrush(QPalette::Window, pixmap);
this.setPalette(palette);
然后你就可以实现调整大小事件,油漆等活动,用你的像素图的背景图片播放..
这篇文章可能有用:https://forum.qt.io/topic/1378/is-it-possible-to-set-a-background-image-to-a-widget/17 – m7913d