How to Set the Background Image in Qt Stylesheet

Unable to set the background image in Qt 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).

Qt stylesheet, background image from filepath

You don't necessarily need add your files as resources. Try this for example:

QFrame{background-image: url("C:/temp/foo.jpg");}

Note the standard slashes, like you'd use in a URL—they're not Windows' back-slashes. So this, for example, will not work:

QFrame{background-image: url("C:\\temp\\foo.jpg");} /* Won't work! */

Windows' back-slashes are invalid in QSS, even when you escape them.

You could also use a relative path:

QFrame{background-image: url("temp/foo.jpg");}

Be careful, though, because the path is relative to the CWD at runtime, which the user might change.

Qt: Setting the background image using stylesheets doesn't work with resources

I think all you needed to do was add a semicolon inside the quotes.

this->setStyleSheet(" background-image: url(C:/test.jpg);");

Background-image in Qt stylesheet doesn't work

From Qt Style Sheets Reference:

Sample Image

It should work. Probably the image file is not to be found where the program expects. If you're using Qt Creator, you should be aware that it builds the target binary in a separate directory (usually with a name like build-yourprojectname-qtversion-Debug or so). That's called shadow build.
You'll need to copy over your image(s) to the proper location relative to that shadow build directory, otherwise the program will not be able to find the file.

Your best bet is to embed the image in the binary as a resource.
I just tried this and seems to be working for me:

In main.cpp:

int main(int argc, char *argv[])
{

QApplication app(argc, argv);

QWidget *w = new QWidget;
w->setStyleSheet("background-image: url(:/resources/pixmaps/close.png);");

w->show();

return app.exec();
}

Note the :/ part in url(:/resources/pixmaps/close.png). That's needed for embedded resources.

In resources.qrc:

<RCC>
<qresource prefix="/">
<file>resources/pixmaps/close.png</file>

... other resource files go here
</qresource>

At the end of testproject.pro:

   RESOURCES += \
resources.qrc

Of course you'll need to put the images in the proper location in your project directory so that the resource compiler can find it.
In my example,

 resources/
├── pixmaps
│   ├── application.png
│   ├── cancel.png
│   ├── close.png <--Here
...

Sample Image

Set a scale for a background-image

Instead you may need to use border-image.

Look into the below content in the link provided.

A background-image does not scale with the size of the widget. To provide a "skin" or background that scales along with the widget size, one must use border-image. Since the border-image property provides an alternate background, it is not required to specify a background-image when border-image is specified. In the case, when both of them are specified, the border-image draws over the background-image.

http://doc.qt.io/qt-5/stylesheet-customizing.html

How to make it in Qt that the MainWindow's background image does not override child widget's

using a stylesheet on a parent widget applies it on all its childs in qt:

so the solution is to specify the widget's class name:

(ex for a QFrame)

frame->setStyleSheet("QFrame{ background-image: url(image_path) }")

or more precisely use widget->setObjectName("objectName"):

widget->setObejectName("objectName");
widget->setStyleSheet("#objectName{ background-image: url(image_path) }")

Qt Stylesheet. Background-color, YES. background-image, NO

The code below is working fine on my machine. Maybe you can see where it differs from what you have? Hope it is helpful.

#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget main_window;
main_window.setStyleSheet("background-image: url(Chrysanthemum.jpg); "
"background-position: top left; "
"background-repeat: repeat-xy;");
main_window.show();
return app.exec();
}


Related Topics



Leave a reply



Submit