2015-06-03 93 views
0

我需要使用QGraphicsSvgItem sublcass来使用svg对象。更改xml文档的根元素(svg)

我正在学习有关SVG - 和我所注意到的是,在SVG显示的罚款,如果它的根元素是<svg ..>

然而,随着W3Schools的样品玩,我注意到嵌入在HTML中所有的例子,它是可能我的代码将不得不处理两种类型(简单的svg,以及包含svg的html)。

所以,我看到的解决方案是,提取svg元素(及其所有的孩子),并用它替换根元素。

在我心中的代码是明确的:

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

唯一的问题是,这是行不通的。我看不到变化。

请帮我提取svg元素并使其成为根...丢弃其他所有元素。

样品来源:

<!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> 

期望的结果:

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

(在!DOCTYPE或任何其他声明可以留)

+0

我不确定这是可能的。一旦一个html文档,我总是一个html文档。 –

+0

@RobertLongson LOL :)))).....然而,应该可以从文档中提取包含其所有子节点的节点 – Thalia

回答

0

一旦我明白 - 基于罗伯特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(); 
    }  
}