无法在Qt样式表中设置背景图片 [英] Unable to set the background image in Qt Stylesheet

查看:1426
本文介绍了无法在Qt样式表中设置背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行我的Qt应用程序通过命令行参数-stylesheet。控件的样式工作,但不是当我试图加载MainWindow的背景图像。
我试过:

I'm running my Qt app through command line with a parameter -stylesheet. The styles for the controls work but not when I'm trying to load a background image for the MainWindow. I tried:

QMainWindow{
background-image:url(:image_256_8bit_latest_back.png);
}

也尝试在后台删除:区别。

Also tried removing the ":" in background, but doesn't make a difference. Can somebody tell me what's wrong with this StyleSheet?

推荐答案

您要使用的图片位置在哪里?

Where is located the image you are trying to use ?

是否将其作为资源

如果您想使用属于资源的图片,您应该有一个资源文件( *。qrc )。此文件应包含如下内容:

If you want to use an image which is part of your resources, you should have a resource file (*.qrc) in your project. This file should contain something like this :

<RCC>
   <qresource prefix="/images">
      <file alias="sunset.jpg">sunset.jpg</file>
   </qresource>
</RCC>



Then, you could write this code in the constructor of your QMainWindow :

setStyleSheet("background-image: url(:/images/sunset.jpg);");

如果您不想使用 Qt资源系统,您只需将磁盘上的映像路径:

If you don't want to use the Qt resource system, you can just put the path to your image on your disk :

setStyleSheet("background-image: url(res/images/sunset.jpg);");

请注意,如果使用相对路径:Qt将从当前位置开始,

Be careful though if you are using a relative path : Qt will start from the current location, which might change, particularly if you are developping with Qt Creator.

使用 Qt Creator ,当您运行应用程序时调试模式,当前路径在 debug / 。当您在发布模式下运行应用程序时,当前路径位于 release / (除非您更改了设置)。

With Qt Creator, when you run your app in debug mode, the current path is in debug/. When you run your app in release mode, the current path is in release/ (unless you changed the settings).

这篇关于无法在Qt样式表中设置背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆