python - QGraphicsSVGItem 忽略(一些)剪切路径。为什么?
问题描述
此 SVG 图像 在 Firefox 和 Inkscape 中正确呈现,但由于某种原因,在QGraphicsSVGItem
没有任何花哨的情况下使用时,它会以这种方式呈现:
作为参考,这是它在 Firefox 上的样子:
如您所见,卡片的背面不应超出白色边框。
难道我做错了什么?有(最好是简单的)修复吗?
MWE:
import sys
from PyQt5 import QtWidgets, QtCore, Qt, QtGui
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
scene.addItem(Qt.QGraphicsSvgItem("back-red.svg"))
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
sys.exit(app.exec())
解决方案
可能您的 svg 不符合 Qt 使用的特性(有关更多信息,请阅读此处)。一种可能的解决方案是使用 QWebEngineView( python -m pip install pyqtwebengine
):
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg, QtWebEngineWidgets
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
filename = os.path.join(CURRENT_DIR, "back-red.svg")
scene = QtWidgets.QGraphicsScene()
renderer = QtSvg.QSvgRenderer(filename)
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
view = QtWebEngineWidgets.QWebEngineView()
view.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
# view.page().setBackgroundColor(QtGui.QColor("transparent"))
view.resize(renderer.viewBox().size())
view.load(QtCore.QUrl.fromLocalFile(filename))
item = scene.addWidget(view)
graphics_view.resize(640, 480)
sys.exit(app.exec())
推荐阅读
- python - 在dict中查找基于键的值长度
- r - 为什么我的前导函数会生成 NA 值?
- junit4 - 如何从 junit 测试生成 spring restdocs 和 Pact.io 协议?
- c++ - 指向成员类型和模板的指针
- recaptcha - 可以使用 api 创建 google recaptcha 站点和密钥吗
- class - 八度类成员变量更改不起作用
- laravel - Laravel 监听 socket.io 客户端发出
- image - 为什么图像未显示在 stackoverflow.com 和“i.stack.imgur.com”错误中
- android - ViewPager with FragmentPagerAdapter 不显示内容
- python - 如何使用循环 [Pandas,Python] 创建由现有 df 中的列组成的多个日期帧?