Horst Walter
Horst Walter

Reputation: 14081

Qt stylesheet, background image from filepath

All examples I have found so far refer to background images in the resource file. Something like:

 QFrame {
     background-image: url(:/images/header.png);
 }

I wonder, is there a way to use a file directly from the file system? Something like:

     background-image: url("C:\temp\foo.jpg"); ????
     background-image: file("C:\temp\foo.jpg"); ????

I have tried all kind of urls, but none is working. Do I always have to add the file in the resources?

Upvotes: 6

Views: 14529

Answers (1)

Jablonski
Jablonski

Reputation: 18524

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.

Upvotes: 14

Related Questions