//建xml文件
QString projectPath = ui->lineEdit->text()+".xml";
QFile projectFile(projectPath);
if(!projectFile.open(QIODevice::ReadWrite|QIODevice::Truncate|QFile::Text))
{
qDebug()<<QStringLiteral("打开文件失败");
}
QDomDocument doc;
QDomProcessingIngInstruction instruction = doc.createProcessingInstruction("xml","version =\"1.0\"encoding =\"UTF-8\"");
if(!instruction.isNull())
{
doc.appendChild(instruction);
//根节点
QDomElement rootElement;
rootElement = doc.createElement("pro");
doc.appendChild(rootElement);
QDomElement elementInfo = doc.createElemrnt("info");
elementInfo.setAttribute("name","peojectInfo");
rootElement.appendChild(elementInfo);
QDomElement elementData = doc.createElement("data");
rootElement.appendChild(elementData);
//遍历treeview节点添加到xml
QStandardItem *model = mytreeView->item(0,0)->child(0,0);
int modelNum = model->rowCount();
for(int i = 0;i<modelNum;i++)
{
if(model->hasChildren())
{
QStandardItem *modelChildItem = model->child(i);
QDomElement modelChildElement = doc.createElement(modelChildItem->text());
elementData.appendChild(modelChildElement);
}
}
QTextStream out(&projectFile);
doc.save(out,2);
projectFile.close();
}
}