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