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>
效果图如下: