Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set the background image in Qt Stylesheet

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?

like image 804
Owen Avatar asked Dec 16 '10 06:12

Owen


2 Answers

Where is located the image you are trying to use ?

Did you put it as a resource of your application ?

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);");

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);");

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.

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).

like image 162
Jérôme Avatar answered Oct 19 '22 05:10

Jérôme


Clearly there is a problem with the path to your image. Try using an absolute path to verify the image is loaded by QT and working.

like image 1
koan Avatar answered Oct 19 '22 04:10

koan