Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QtSvg忽略单位_Python_Svg_Pyside2_Qtsvg - Fatal编程技术网

Python QtSvg忽略单位

Python QtSvg忽略单位,python,svg,pyside2,qtsvg,Python,Svg,Pyside2,Qtsvg,有没有办法让QSvgWidget/qsvgrender与cm/mm/in/pc这样的单位一起工作 下面是一个示例SVG,其中所有行的长度都应该相同(它在Firefox和Chrome中正确呈现): 这是我的观众: 从PySide2导入QtWidgets、QtSvg 导入PySide2 app=qtwidts.QApplication(sys.argv) w=QtSvg.QSvgWidget(“demo.svg”) w、 show() sys.exit(app.exec_()) 如果我用这个查

有没有办法让QSvgWidget/qsvgrender与cm/mm/in/pc这样的单位一起工作

下面是一个示例SVG,其中所有行的长度都应该相同(它在Firefox和Chrome中正确呈现):


这是我的观众:

从PySide2导入QtWidgets、QtSvg
导入PySide2
app=qtwidts.QApplication(sys.argv)
w=QtSvg.QSvgWidget(“demo.svg”)
w、 show()
sys.exit(app.exec_())
如果我用这个查看器打开SVG,QT似乎会忽略所有单位,并将属性读取为
x2=“105.82mm”
as
x2=“105.82”

导致Qt:

导致Firefox:

QT和PySide2都是5.14.1版

说明: 通过分析QSvgHandler的源代码,我发现只有长度(宽度和高度属性)支持单位,而不是坐标,要理解它,分析以下两个代码片段就足够了:

//https://code.qt.io/cgit/qt/qtsvg.git/tree/src/svg/qsvghandler.cpp#n2816
静态QSvgNode*createLineNode(QSvgNode*parent,
常量QXmlStreamAttributes和attributes,
QSvgHandler*)
{
常量QStringRef x1=attributes.value(QLatin1String(“x1”);
常量QStringRef y1=attributes.value(QLatin1String(“y1”);
常量QStringRef x2=attributes.value(QLatin1String(“x2”);
const QStringRef y2=attributes.value(QLatin1String(“y2”));
qreal nx1=toDouble(x1);
qreal ny1=toDouble(y1);
qreal nx2=toDouble(x2);
qreal ny2=toDouble(y2);
QLineF lineBounds(nx1,ny1,nx2,ny2);
QSvgNode*line=新的QSvgLine(父,lineBounds);
回流线;
}
//https://code.qt.io/cgit/qt/qtsvg.git/tree/src/svg/qsvghandler.cpp#n3054
静态QSvgNode*createRectNode(QSvgNode*parent,
常量QXmlStreamAttributes和attributes,
QSvgHandler*处理器)
{
常量QStringRef x=attributes.value(QLatin1String(“x”);
常量QStringRef y=attributes.value(QLatin1String(“y”);
常量QStringRef width=attributes.value(QLatin1String(“width”);
常量QStringRef height=attributes.value(QLatin1String(“height”);
常量QStringRef rx=attributes.value(QLatin1String(“rx”);
常量QStringRef ry=attributes.value(QLatin1String(“ry”);
QSvgHandler::LengthType类型;
qreal nwidth=parseLength(宽度、类型、处理程序);
nwidth=convertToPixels(nwidth,true,type);
qreal nheight=parseLength(高度、类型、处理程序);
nheight=转换像素(nheight,true,type);
qreal nrx=toDouble(rx);
qreal nry=toDouble(ry);
QRectF界(toDouble(x),toDouble(y),
nwidth,nheight);
//9.2“rect”元素明确规定了这一点
//但事实上,这个案件可能会得到处理,因为
//我们画圆形矩形的方式不同
如果(nrx>bounds.width()/2)
nrx=bounds.width()/2;
如果(nry>bounds.height()/2)
nry=bounds.height()/2;
如果(!rx.isEmpty()&&ry.isEmpty())
nry=nrx;
如果(!ry.isEmpty()&&rx.isEmpty())
nrx=nry;
//我们从0…99画四舍五入的矩形
//svg来自0…bounds.width()/2,因此我们正在调整
//坐标
nrx*=(100/(bounds.width()/2));
nry*=(100/(bounds.height()/2));
QSvgNode*rect=新的QSvgRect(父项、边界、,
整数(nrx),
int(nry));
返回矩形;
}

转换单位的方法是,仅在“宽度”和“高度”中使用

解决方案: 解决方法是使用QWebEngineView:

导入操作系统
导入系统
从PySide2导入QtCore、QtWidgets、QtSvg、QtWebEngineWidgets
CURRENT_DIR=os.path.dirname(os.path.realpath(uu文件_uu))
如果名称=“\uuuuu main\uuuuuuuu”:
app=qtwidts.QApplication(sys.argv)
filename=os.path.join(当前目录“demo.svg”)
qsvgwidget=QtSvg.qsvgwidget(文件名)
qwebengineview=QtWebEngineWidgets.qwebengineview()
qwebengineview.load(QtCore.qrl.fromLocalFile(文件名))
w=qtwidts.QWidget()
lay=qtwidts.QHBoxLayout(w)
lay.addWidget(qsvgwidget,strecth=1)
lay.addWidget(qwebengineview,strecth=1)
w、 show()
sys.exit(app.exec_())

谢谢,看起来好多了。您是否知道如何将QWebEngineView渲染到pixmap?因为我在一开始研究SVG的原因是我想将它们包括在我用QPrinter创建的报告中。@rmweiss尝试一下:转换单位的方法是parseLength(),它只用于“宽度”和“高度”。-为什么会有人这么做?