2015-06-03 92 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(); 
    }  
}