用C++保存QTreeWidget三层六列数据到xml,再读取xml显示为QTreeWidget
时间: 2024-04-29 17:24:15 浏览: 182
以下是一个示例代码,展示如何使用Qt的QDomDocument类保存和读取一个QTreeWidget的数据到xml文件中:
```c++
#include <QtWidgets>
void saveTreeWidgetToXml(QTreeWidget* treeWidget, const QString& fileName) {
QDomDocument doc;
QDomElement root = doc.createElement("TreeWidget");
doc.appendChild(root);
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem* topLevelItem = treeWidget->topLevelItem(i);
QDomElement topLevelElement = doc.createElement("TopLevelItem");
topLevelElement.setAttribute("text", topLevelItem->text(0));
root.appendChild(topLevelElement);
for (int j = 0; j < topLevelItem->childCount(); ++j) {
QTreeWidgetItem* childItem = topLevelItem->child(j);
QDomElement childElement = doc.createElement("ChildItem");
childElement.setAttribute("text", childItem->text(0));
topLevelElement.appendChild(childElement);
for (int k = 1; k < treeWidget->columnCount(); ++k) {
QDomElement columnElement = doc.createElement("Column");
columnElement.setAttribute("index", k);
columnElement.setAttribute("text", childItem->text(k));
childElement.appendChild(columnElement);
}
}
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Failed to open file for writing: " << fileName;
return;
}
QTextStream stream(&file);
stream << doc.toString();
file.close();
}
void loadTreeWidgetFromXml(QTreeWidget* treeWidget, const QString& fileName) {
QDomDocument doc;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open file for reading: " << fileName;
return;
}
if (!doc.setContent(&file)) {
qDebug() << "Failed to parse XML document.";
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
if (root.tagName() != "TreeWidget") {
qDebug() << "Invalid root element: " << root.tagName();
return;
}
treeWidget->clear();
QDomNodeList topLevelNodes = root.elementsByTagName("TopLevelItem");
for (int i = 0; i < topLevelNodes.count(); ++i) {
QDomElement topLevelElement = topLevelNodes.at(i).toElement();
QTreeWidgetItem* topLevelItem = new QTreeWidgetItem(treeWidget);
topLevelItem->setText(0, topLevelElement.attribute("text"));
QDomNodeList childNodes = topLevelElement.elementsByTagName("ChildItem");
for (int j = 0; j < childNodes.count(); ++j) {
QDomElement childElement = childNodes.at(j).toElement();
QTreeWidgetItem* childItem = new QTreeWidgetItem(topLevelItem);
childItem->setText(0, childElement.attribute("text"));
QDomNodeList columnNodes = childElement.elementsByTagName("Column");
for (int k = 0; k < columnNodes.count(); ++k) {
QDomElement columnElement = columnNodes.at(k).toElement();
int column = columnElement.attribute("index").toInt();
QString text = columnElement.attribute("text");
childItem->setText(column, text);
}
}
}
}
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QTreeWidget* treeWidget = new QTreeWidget;
treeWidget->setColumnCount(6);
treeWidget->setHeaderLabels(QStringList() << "Column 1" << "Column 2" << "Column 3" << "Column 4" << "Column 5" << "Column 6");
// Add some data to the tree widget
QTreeWidgetItem* topLevelItem1 = new QTreeWidgetItem(treeWidget);
topLevelItem1->setText(0, "Top Level Item 1");
QTreeWidgetItem* childItem1 = new QTreeWidgetItem(topLevelItem1);
childItem1->setText(0, "Child Item 1");
childItem1->setText(1, "Data 1");
childItem1->setText(2, "Data 2");
childItem1->setText(3, "Data 3");
childItem1->setText(4, "Data 4");
childItem1->setText(5, "Data 5");
QTreeWidgetItem* childItem2 = new QTreeWidgetItem(topLevelItem1);
childItem2->setText(0, "Child Item 2");
childItem2->setText(1, "Data 6");
childItem2->setText(2, "Data 7");
childItem2->setText(3, "Data 8");
childItem2->setText(4, "Data 9");
childItem2->setText(5, "Data 10");
QTreeWidgetItem* topLevelItem2 = new QTreeWidgetItem(treeWidget);
topLevelItem2->setText(0, "Top Level Item 2");
QTreeWidgetItem* childItem3 = new QTreeWidgetItem(topLevelItem2);
childItem3->setText(0, "Child Item 3");
childItem3->setText(1, "Data 11");
childItem3->setText(2, "Data 12");
childItem3->setText(3, "Data 13");
childItem3->setText(4, "Data 14");
childItem3->setText(5, "Data 15");
saveTreeWidgetToXml(treeWidget, "treeWidget.xml");
loadTreeWidgetFromXml(treeWidget, "treeWidget.xml");
treeWidget->show();
return app.exec();
}
```
阅读全文