Now let’s write a program that adds nodes freely. Content or continue above a few chapters.
Start with the lowest NodeTS class and build a constructor:
`NodeTS::NodeTS(double argNormalizedIntensity, double argOpacity, QColor argEmission, QObject * parent):CommonXML(parent){ this->NormalizedIntensity = argNormalizedIntensity; this->Opacity = argOpacity; this->Emission = argEmission; } `Copy the code
Our final plan is to implement a function like this:
`TsfunGroup tg; tg.readXML(""); NodeTS NTS (0.01923, 0.1023, QColor (123,32,67)); tg.addNodeToItem("maxMode",nts); tg.writeXML(); `Copy the code
First read the file, then create a new NodeTS object, and then add the node to tsfunItem with id maxMode. We then write the set nodes to the XML file.
In order to implement such a function, it is necessary to first be able to NodeTsList TsNodes in the TsfunItem class; Adds the new node to the member. The procedure is as follows:
`void TsfunItem::addNode(NodeTS nts) { double temp1,temp2; for (int i = 0; i < TsNodes.count()-1; i++) { temp1 = TsNodes[i].returnNormalizedIntensity(); temp2 = TsNodes[i+1].returnNormalizedIntensity(); If (NTS) returnNormalizedIntensity () > temp1) {if (NTS) returnNormalizedIntensity () < temp2) {/ / can be inserted here. TsNodes.insert(i,nts); break; `}}}}Copy the code
Notice the order, we need to determine in what order the nodes should be inserted based on NormalizedIntensity, and then we just insert them.
Then inside the TsfunGroup class, define the function:
`void TsfunGroup::addNodeToItem(QString itemName, NodeTS nts) { for (int i = 0; i < tsfitmlist.count(); i++) { if (itemName == tsfitmlist[i].returnNameID()) { tsfitmlist[i].addNode(nts); `}}}Copy the code
Find the node we want to insert according to itemName, insert the node.
The resulting display looks like this:
'<Nodes> <Node> <NormalizedIntensity Value="0.01923"/> <Opacity Value="0.1023"/> <Emission ="123" B="67" G="32"/> </Node> <Node> <NormalizedIntensity Value="0"/> <Opacity Value="0"/> <Emission R="0" B="0" G="0"/> </Node> <Node> <NormalizedIntensity Value="0.13597"/> <Opacity Value="0"/> <Emission R="0" B="0" G="0"/> </Node> <Node> <NormalizedIntensity Value="0.188538"/> <Opacity Value="0.0279028"/> <Emission R="0" B="0" G="0"/> </Node> <Node> <NormalizedIntensity Value="0.288578"/> <Opacity Value="0.363502"/> <Emission R="0" B="0" G="0"/> </Node> <Node> <NormalizedIntensity Value="0.45417"/> <Opacity Value="1"/> <Emission R="0" B="0" G="0"/> </Node> <Node> <NormalizedIntensity Value="1"/> <Opacity Value="1"/> <Emission R="0" B="0" G="0"/> </Node> </Nodes>`Copy the code
You can see that the number of children has increased from six to seven, indicating that we have added the right ones.