QGraphicsSVGItem 忽略(一些)剪切路径.为什么?

QGraphicsSVGItem ignores (some) clipping paths. Why?(QGraphicsSVGItem 忽略(一些)剪切路径.为什么?)
本文介绍了QGraphicsSVGItem 忽略(一些)剪切路径.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

).一种可能的解决方案是使用 QWebEngineView(python -m pip install pyqtwebengine):

导入操作系统导入系统从 PyQt5 导入 QtCore、QtGui、QtWidgets、QtSvg、QtWebEngineWidgetsCURRENT_DIR = os.path.dirname(os.path.realpath(__file__))如果 __name__ == '__main__':应用程序 = QtWidgets.QApplication(sys.argv)文件名 = os.path.join(CURRENT_DIR, "back-red.svg")场景 = QtWidgets.QGraphicsScene()渲染器 = QtSvg.QSvgRenderer(文件名)graphics_view = QtWidgets.QGraphicsView()graphics_view.setScene(场景)graphics_view.show()视图 = QtWebEngineWidgets.QWebEngineView()view.setContextMenuPolicy(QtCore.Qt.NoContextMenu)# view.page().setBackgroundColor(QtGui.QColor("透明"))view.resize(renderer.viewBox().size())view.load(QtCore.QUrl.fromLocalFile(文件名))item = scene.addWidget(view)graphics_view.resize(640, 480)sys.exit(app.exec())

This SVG image is correctly rendered in Firefox and Inkscape, but for some reason, when using QGraphicsSVGItem without anything fancy, it renders this way:

For reference, this is what it looks like on firefox:

As you can see, the back of the card is not supposed to go beyond the white border.

Am I doing something wrong? Is there a (preferably easy) fix?

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

解决方案

Probably your svg does not meet the characteristics that Qt uses (for more information read here). One possible solution is to use 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())

这篇关于QGraphicsSVGItem 忽略(一些)剪切路径.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)