我有一个方法,它解析XML并在QtreeQWidget
中显示它。但是,它的代码没有看到子女,例如:
<?xml version="1.0"?>
<Programs>
<Iznos>
<Iznos>3.2.3.7</Iznos>
<Iznos>3.2.3.8</Iznos>
</Iznos>
<KIV_VIKS>
<ISA_ver/>
<ISA_ver>2.0.5.10</ISA_ver>
<ISA_ver>2.0.5.7</ISA_ver>
<ISA_ver>2.0.5.8</ISA_ver>
<ISA_ver>2.0.5.9</ISA_ver>
<PCI_ver/>
<PCI_ver>3.0.1.1</PCI_ver>
<PCI_ver>3.0.1.7</PCI_ver>
</KIV_VIKS>
<Reader>
<Reader>4.3.0.23</Reader>
<Reader>4.3.0.24</Reader>
<Reader>4.3.1.3</Reader>
<Reader>4.3.1.9</Reader>
</Reader>
</Programs>
&#13;
我的代码:
void MainWindow::visionListVersion(QDomElement child, QTreeWidgetItem *parentItem, int index_check)
{
QDomElement tempElem;
if(child.parentNode().nodeName()=="Programs"){
QTreeWidgetItem *tmpSecItem = new QTreeWidgetItem(QStringList()<<child.nodeName());
ui->treeWidget->addTopLevelItem(tmpSecItem);
MainWindow::visionListVersion( child.firstChildElement(), ui->treeWidget->topLevelItem(topI++), index_check);
}
else if(child.hasChildNodes()){
QTreeWidgetItem *tmpSecItem = new QTreeWidgetItem(QStringList()<<child.nodeName() + " " + child.text());
tmpSecItem->setFlags(tmpSecItem->flags() | Qt::ItemIsUserCheckable |Qt::ItemIsSelectable);
tmpSecItem->setCheckState(index_check, Qt::Unchecked);
QFile file("Client.xml");
if(file.open(QIODevice::ReadOnly)){
QXmlStreamReader sr(&file);
do{
sr.readNext();
if(sr.text() == child.text()) tmpSecItem->setCheckState(index_check, Qt::Checked);
}while(!sr.atEnd());
}
parentItem->addChild(tmpSecItem);
int index;
if(parentItem->childCount()==0)index=0;
else index=parentItem->childCount()-1;
visionListVersion( child.firstChildElement(), parentItem->child(index), index_check);
}
tempElem = child.nextSiblingElement();
if(!tempElem.isNull())MainWindow::visionListVersion(tempElem, parentItem, index_check++);
}
我不明白如何更新代码以解决我的问题。 请帮帮我,谢谢。