用C++保存QTreeWidget到xml,再读取xml显示为QTreeWidget
时间: 2024-05-14 09:14:09 浏览: 18
以下是一个示例代码,可以将QTreeWidget保存为xml文件,并从xml文件中读取并显示为QTreeWidget。
保存为xml:
```
#include <QtXml>
// 保存QTreeWidget为xml文件
void saveTreeWidgetToXml(QTreeWidget* treeWidget, const QString& fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return;
QDomDocument doc;
QDomElement root = doc.createElement("tree");
doc.appendChild(root);
for (int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem* item = treeWidget->topLevelItem(i);
QDomElement element = doc.createElement("item");
element.setAttribute("text", item->text(0));
for (int j = 0; j < item->childCount(); j++)
{
QTreeWidgetItem* childItem = item->child(j);
QDomElement childElement = doc.createElement("item");
childElement.setAttribute("text", childItem->text(0));
element.appendChild(childElement);
}
root.appendChild(element);
}
QTextStream stream(&file);
stream << doc.toString();
file.close();
}
```
从xml中读取:
```
#include <QtXml>
// 从xml文件中读取QTreeWidget
void readTreeWidgetFromXml(QTreeWidget* treeWidget, const QString& fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return;
QDomDocument doc;
if (!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
if (root.tagName() != "tree")
return;
treeWidget->clear();
QDomNodeList topLevelItems = root.childNodes();
for (int i = 0; i < topLevelItems.count(); i++)
{
QDomNode topLevelNode = topLevelItems.at(i);
if (!topLevelNode.isElement())
continue;
QDomElement topLevelElement = topLevelNode.toElement();
QTreeWidgetItem* topLevelItem = new QTreeWidgetItem(treeWidget, QStringList(topLevelElement.attribute("text")));
QDomNodeList childItems = topLevelElement.childNodes();
for (int j = 0; j < childItems.count(); j++)
{
QDomNode childNode = childItems.at(j);
if (!childNode.isElement())
continue;
QDomElement childElement = childNode.toElement();
new QTreeWidgetItem(topLevelItem, QStringList(childElement.attribute("text")));
}
}
}
```
使用示例:
```
// 保存为xml
QTreeWidget* treeWidget = ...; // 创建并添加item
saveTreeWidgetToXml(treeWidget, "tree.xml");
// 从xml中读取
QTreeWidget* newTreeWidget = new QTreeWidget(parent);
readTreeWidgetFromXml(newTreeWidget, "tree.xml");
```
注意:在保存和读取xml时,需要确保文件名和文件路径正确。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)