【问题标题】:How to reference resource file in PyQt stylesheet using fbs如何使用 fbs 在 PyQt 样式表中引用资源文件
【发布时间】:2019-09-19 16:44:33
【问题描述】:

我正在使用 fbs 构建一个 PyQt5 应用程序,并且 (following this tutorial) 已将图像文件放在目录 /src/main/resources/base/images 中。通过这种方式,图像资源可用于使用ApplicationContext.get_resource("images/xxxx.png") 的 Python 代码。构建系统根据我们是从源代码运行还是从应用的编译版本运行来管理正确的文件路径。

我想在我的 PyQt 样式表中访问这些图像,按照

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

但是,上面的代码没有显示图像。

有没有办法从 PyQt5 样式表中引用 fbs 资源?

【问题讨论】:

    标签: python pyqt5 fbs


    【解决方案1】:

    当你使用“:”时,假设你使用的是一个qresource但fbs没有使用它,所以你必须使用本地路径:

    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)
    

    【讨论】:

    • 谢谢,效果很好!我需要添加到您的代码中的唯一一件事是确保图像路径使用正斜杠:from pathlib import Pathbranch_icon = Path(appctxt.get_resource("images/branch-closed.png")).as_posix()。我在 Windows 上运行,并且没有转换为 posix 路径有反斜杠,这不适用于 QSS(根据 stackoverflow.com/a/26121846)。
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2021-10-22
    • 2023-03-16
    • 1970-01-01
    • 2016-07-04
    • 2019-01-02
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多