如何使用QT创建具有pair标记的Child

时间:2016-07-12 07:35:36

标签: c++ xml qt


 我正在开发一个项目,我必须使用QT库(QTDomDocument等)为C ++在XML文档中写入信息。
 这可能是愚蠢的问题,但我正在寻找一个关于如何使用pair标签创建XML子节点的解决方案,例如,我有这个:

<color_space>
</color_space>

我想在这个架构之后添加x个孩子:

<color_space>
   <color_plan>R</color_plan>
   <color_plan>G</color_plan>
    ...
</color_space>

我尝试了多种不同的代码,就我所做的那样,我得到的结果最为相似:

<color_space>
   <color_plan/>
   <color_plan/>
    ...
</color_space>

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:1)

好的,所以我只是弄清楚如何做我想做的事情,我希望它能帮助其他人!

    // Here we have a QVector where we have all our color plan
       QVector<QString>ColorPlanTable= { "R","G","B","L","a","b","Y","Cb","Cr" };

    //sub_element is the Color_space element (parent node)
      sub_element= sub_element.nextSiblingElement();

    // Here we create a new element for color_plan (child of Color_space)
      QDomElement NewColorPlan=document_->createElement("color_plan");

    // And then we create a TextNode to add to the element color_plan
      QDomText NewColorPlanText = document_->createTextNode(ColorPlanTable.at(i));

    // Adding the element color_plan as a child of Color_space
      sub_element.appendChild(NewColorPlan);

    // And then adding the text of the color_plan!
      NewColorPlan.appendChild(NewColorPlanText);