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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With