QTreeView读取XML文件实现顶层条目和子条目显示,并动态获取

QTreeView读取XML文件实现顶层条目和子条目:

#pragma once

#include <QtWidgets/QWidget>
#include "ui_C_ParamXmlReadDisplay.h"
#include "qcombobox.h"
#include "qdom.h"
#include "qtreeview.h"
#include "qstandarditemmodel.h"
#include "qlayout.h"
#include "qfile.h"

class C_ParamXmlReadDisplay : public QWidget
{
    Q_OBJECT

public:
    C_ParamXmlReadDisplay(QWidget *parent = nullptr);

private slots:
    void onComboBoxIndexChanged(int index);

private:
    void loadXmlData();

    //void updateTreeView(const QString& elementName);

    QComboBox* combo_box;
    QTreeView* tree_view;
    QStandardItemModel tree_model;
    QDomDocument doc;


private:
    Ui::C_ParamXmlReadDisplayClass ui;
};
#include "C_ParamXmlReadDisplay.h"

C_ParamXmlReadDisplay::C_ParamXmlReadDisplay(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    QVBoxLayout* layout = new QVBoxLayout(this);
    combo_box = new QComboBox;
    tree_view = new QTreeView;

    layout->addWidget(combo_box);
    layout->addWidget(tree_view);

    connect(combo_box, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &C_ParamXmlReadDisplay::onComboBoxIndexChanged);

    loadXmlData(); // 初始化并刷新QComboBox
}

void C_ParamXmlReadDisplay::loadXmlData()
{
    QFile file("ParamXml.xml"); // 替换为您的实际XML文件路径
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return;
    }

    if (!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement root = doc.documentElement();
    QDomNodeList children = root.childNodes();
    for (int i = 0; i < children.count(); i++)
    {
        QDomNode child = children.at(i);
        if (child.isElement())
        {
            QDomElement element = child.toElement();
            QString elementName = element.tagName();
            combo_box->addItem(elementName);
        }
    }
}

void C_ParamXmlReadDisplay::onComboBoxIndexChanged(int index)
{
    QString selectedElement = combo_box->itemText(index);
    QDomElement root = doc.documentElement();
    QDomNodeList children = root.elementsByTagName(selectedElement);
    QDomElement selectedElementNode = children.at(0).toElement();

    tree_model.clear();
    tree_model.setHorizontalHeaderLabels(QStringList() << "Attribute" << "Value");
    tree_view->setModel(&tree_model);

    QDomNodeList subChildren = selectedElementNode.childNodes();
    for (int i = 0; i < subChildren.count(); i++)
    {
        QDomNode subChild = subChildren.at(i);
        if (subChild.isElement())
        {
            QDomElement subElement = subChild.toElement();
            QStandardItem* parentItem = new QStandardItem(subElement.tagName());
            QDomNamedNodeMap attributes = subElement.attributes();
            for (int j = 0; j < attributes.count(); j++)
            {
                QDomNode attribute = attributes.item(j);
                QString key = attribute.nodeName();
                QString value = attribute.nodeValue();
                QList<QStandardItem*> childItems;
                childItems << new QStandardItem(key) << new QStandardItem(value);
                parentItem->appendRow(childItems);
            }
            tree_model.appendRow(parentItem);
        }
    }
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RootParam>
    <PointAll Index="4" N="Pointing" Test="P.bmp">
        <NewChild Index_1="6" N="Pointing" Test="P.bmp"/>
        <NewChild_2 Index_3="4" N="Pointing" Test="P.bmp"/>
        <NewChild_3 Index="4" Test="P.bmp"/>
        <存图 Index="4" Test="P.bmp"/>
    </PointAll>
    <PointAll_2 Index="4" N="Pointing" Test="P.bmp">
        <NewChild Index_1="6" N="Pointing" Test="P.bmp"/>
        <NewChild_2 Index_3="4" N="Pointing" Test="P.bmp"/>
        <NewChild_3 Index="4" Test="P.bmp"/>
        <存图 Index="4" Test="P.bmp"/>
    </PointAll_2>
    <PointAll_3 Index="4" N="Pointing" Test="P.bmp">
        <NewChild Index_1="6" N="Pointing" Test="P.bmp"/>
        <NewChild_2 Index_3="4" N="Pointing" Test="P.bmp"/>
        <NewChild_3 Index="4" Test="P.bmp"/>
        <存图 Index="4" Test="P.bmp"/>
    </PointAll_3>
</RootParam>

效果图如下:

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值