如何使用 fbs 在 PyQt 样式表中引用资源文件 [英] How to reference resource file in PyQt stylesheet using fbs

查看:70
本文介绍了如何使用 fbs 在 PyQt 样式表中引用资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

推荐答案

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

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)

这篇关于如何使用 fbs 在 PyQt 样式表中引用资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆