1

I am building a PyQt5 application using fbs and (following this tutorial) have placed the image files in the directory /src/main/resources/base/images. This way the image resources are available to the Python code using ApplicationContext.get_resource("images/xxxx.png"). The build system manages the correct file path depending on whether we are running from source or the compiled version of the app.

I would like to access these images in my PyQt stylesheets, along the lines of

QTreeView::branch:closed:has-children:has-siblings {
    border-image: none;
    image: url(:/images/branch-closed.png);

however, the above code doesn't display the image.

Is there a way to reference fbs resources from within PyQt5 stylesheets?

1 Answers1

2

When you use ":" it is assumed that you are using a qresource but fbs does not use it, so you must use the local path:

QSS = '''
QTreeView::branch:closed:has-children:has-siblings{
    border-image: none;
    image: url(%(branch_icon)s);
}
'''

# ...
appctxt = ApplicationContext()
branch_icon = appctxt.get_resource("images/branch-closed.png")
qss = QSS % {"branch_icon": branch_icon}
appctxt.app.setStyleSheet(qss)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    Thanks, that worked great! The only thing I needed to add to your code was to ensure that the image path was using forward slashes: `from pathlib import Path` and `branch_icon = Path(appctxt.get_resource("images/branch-closed.png")).as_posix()`. I'm running on Windows, and without converting to posix the path has back-slashes, which don't work with QSS (as per https://stackoverflow.com/a/26121846). – user1972265 May 01 '19 at 10:11