Qt stylesheet, background image from filepath

巧了我就是萌 提交于 2019-11-28 05:25:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/26121737/qt-stylesheet-background-image-from-filepath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!