I need to work with svg objects using a QGraphicsSvgItem
sublcass.
我需要使用QGraphicsSvgItem sublcass处理svg对象。
I am learning about svg - and what I have noticed is, the svg shows fine if its root element is <svg ..>
我正在学习svg - 我注意到的是,如果它的根元素是
Yet, playing with w3schools samples, I noticed all their examples embedded in html, and it is possible that my code will have to process both kinds (simple svg, as well as html containing svg).
然而,在玩w3schools样本时,我注意到他们的所有示例都嵌入在html中,并且我的代码可能必须处理这两种类型(简单的svg,以及包含svg的html)。
So, the solution as I see it is, extract the svg element (with all its kids) and replace the root element with it.
所以,我看到的解决方案是,提取svg元素(及其所有孩子)并用它替换根元素。
In my mind the code is clear:
在我看来,代码很清楚:
QDomDocument _svgXML;
void setContentFromFile(QString filename)
{
QFile file(filename);
file.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&file);
QString data = in.readAll();
file.close();
_svgXML.setContent(svgContent);
checkRoot();
}
void checkRoot()
{
QDomElement rootElem = _svgXML.documentElement();
recursivelyCheckRoot(rootElem);
qDebug(qPrintable(QString("root ") + rootElem.nodeName()));
// or
qDebug(_svgXML.toByteArray());
}
void recursivelyCheckRoot(QDomElement& rootElem)
{
if(rootElem.nodeName() == "svg")
return;
QDomNode n = rootElem.firstChild();
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.nodeName() == "svg")
{
rootElem = e; return;
}
recursivelyCheckRoot(e);
}
n = n.nextSibling();
}
}
The only problem is that it doesn't work. I see no change.
唯一的问题是它不起作用。我看不出变化。
Please help me extract the svg element and make it root... discarding all else.
请帮我提取svg元素并使其成为root ...丢弃所有其他元素。
Sample source:
<!DOCTYPE html>
<html>
<body>
<svg width="400" height="150">
<defs>
<linearGradient id="grad1" y1="0%" x1="0%" y2="0%" x2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
</linearGradient>
</defs>
<ellipse fill="url(#grad1)" cx="200" cy="70" rx="85" ry="55"/>
<text x="150" y="86" fill="green" font-family="Verdana" font-size="45">SVG</text>
Sorry, your browser does not support inline SVG.
</svg>
</body>
</html>
Desired result:
<svg width="400" height="150">
<defs>
<linearGradient id="grad1" y1="0%" x1="0%" y2="0%" x2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
</linearGradient>
</defs>
<ellipse fill="url(#grad1)" cx="200" cy="70" rx="85" ry="55"/>
<text x="150" y="86" fill="green" font-family="Verdana" font-size="45">SVG</text>
Sorry, your browser does not support inline SVG.
</svg>
(the !DOCTYPE
or any other declarations could stay)
(!DOCTYPE或任何其他声明可以保留)
1 个解决方案
#1
0
Once I understood - based on Robert Longson's comment "Once an html document, always an html document" ... I realized that I cannot modify an existing document, I had to make a new one.
一旦我理解了 - 基于Robert Longson的评论“曾经是一个HTML文档,总是一个html文档”......我意识到我无法修改现有文档,我不得不重新编写。
void recursivelyCheckRoot(QDomElement rootElem)
{
if(rootElem.nodeName() == "svg")
return;
QDomNode n = rootElem.firstChild();
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.nodeName() == "svg")
{
QString str;
QTextStream stream(&str);
e.save(stream, QDomNode::DocumentNode);
_svgXML.setContent(str);
return;
}
recursivelyCheckRoot(e);
}
n = n.nextSibling();
}
}
#1
0
Once I understood - based on Robert Longson's comment "Once an html document, always an html document" ... I realized that I cannot modify an existing document, I had to make a new one.
一旦我理解了 - 基于Robert Longson的评论“曾经是一个HTML文档,总是一个html文档”......我意识到我无法修改现有文档,我不得不重新编写。
void recursivelyCheckRoot(QDomElement rootElem)
{
if(rootElem.nodeName() == "svg")
return;
QDomNode n = rootElem.firstChild();
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.nodeName() == "svg")
{
QString str;
QTextStream stream(&str);
e.save(stream, QDomNode::DocumentNode);
_svgXML.setContent(str);
return;
}
recursivelyCheckRoot(e);
}
n = n.nextSibling();
}
}