Qt Creator,如何将图像设置为背景图片

时间:2017-06-20 10:42:36

标签: image qt background qt-creator

我只想将我的qt creater窗口的背景设置为我保存在计算机上的图片。我尝试右键单击背景,进入Change styleSheet,然后点击添加资源但没有出现,我无法在计算机上搜索该文件。

1 个答案:

答案 0 :(得分:0)

将文件添加到 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);

然后你可以实现调整大小事件,绘制事件等,以播放像素图背景图像..