Search code examples
pythonpyqt5fbs

How to reference resource file in PyQt stylesheet using fbs


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?


Solution

  • 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)