首页 > 其他分享 >xml qtreewidget 的遍历

xml qtreewidget 的遍历

时间:2023-06-07 14:24:00浏览次数:50  
标签:xml pItem 遍历 childElement doc setAttribute QString qtreewidget

这些都是自己工作中遇到的,不具有普遍性

 

xml的递归遍历

void UserTreeWidget::travelDomElement(QDomElement & ele, QStringList& listOuterId)
{
    QDomNode node = ele.firstChild();
    while (!node.isNull())
    {
        QDomElement childElement = node.toElement(); // try to convert the node to an element.
        if (!childElement.isNull())
        {
            QString objId = childElement.attribute("ID");
            QString parentId = childElement.attribute("ParentID");
            QString name = childElement.attribute("Name");
            QString itemIcon = childElement.attribute("ItemIcon");

            QString tagName = childElement.tagName();
            if (tagName == "Folder")
            {
                travelDomElement(childElement, listOuterId);
            }
            else if (tagName == "Object")
            {
                listOuterId.push_back(objId);
            }
        }
        node = node.nextSibling();
    }
}

qtreewidget遍历保存到xml

void UserTreeWidget::slotSaveUserData(const QString &filePath) {
    if(filePath.isEmpty())
        return;

    QDomDocument doc("xml");
    QDomElement root = doc.createElement("UserData");
    doc.appendChild(root);

    int topCount = ui->treeWidget->topLevelItemCount();
    for (int i = 0; i < topCount; i++)
    {
        QTreeWidgetItem *pItem = ui->treeWidget->topLevelItem(i);
        saveTreeItem(pItem,doc,root);
    }
    
    processUdtXml(doc, root, filePath);

    QString xml = doc.toString();
    QFile file(filePath);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        file.write(xml.toUtf8());
        file.close();
    }
}

 

void UserTreeWidget::saveTreeItem(const QTreeWidgetItem *pItem,QDomDocument & doc,QDomElement &element){
    int itemType = pItem->data(0, ITEM_TYPE_ROLE).toInt();
    QString objId = pItem->data(0, OBJ_ID_ROLE).toString();
    QString objOuterId = pItem->data(0, OBJ_OUTERID_ROLE).toString();
    QString objParentId = pItem->data(0, OBJ_PARENT_ID_ROLE).toString();
    QString itemIcon = pItem->data(0, ITEM_ICON).toString();
    QString name = pItem->text(0);

    if (itemType == TREE_ITEM_DATA_TYPE_FOLDER)
    {
        QDomElement folderDom = doc.createElement("Folder");
        folderDom.setAttribute("ID", objId);
        if (!objParentId.isEmpty())
            folderDom.setAttribute("ParentID", objParentId);
        folderDom.setAttribute("Name",name);
        folderDom.setAttribute("ItemIcon", itemIcon);
        element.appendChild(folderDom);

        int childCount = pItem->childCount();
        for (int i = 0; i < childCount; i++)
        {
            QTreeWidgetItem *childItem = pItem->child(i);
            saveTreeItem(childItem,doc,folderDom);
        }
    }
    else
    {
        QDomElement objDom = doc.createElement("Object");
        objDom.setAttribute("Name", name);
        objDom.setAttribute("ID", objOuterId);
        objDom.setAttribute("ItemIcon", itemIcon);
        if (!objParentId.isEmpty())
            objDom.setAttribute("ParentID", objParentId);
        element.appendChild(objDom);
    }
}

 

标签:xml,pItem,遍历,childElement,doc,setAttribute,QString,qtreewidget
From: https://www.cnblogs.com/warmlight/p/17463151.html

相关文章