QT实现将用户选择目录以目录树的形式展示并保存为XML
注意:这是一个demo,主要功能是,用户自定义选择一个文件夾,在QTreeWidget控件中展示这个文件夹的树形结构,并且提供了将该树结构存储成xml文件的功能。
demo界面:
点击选择目录按钮,可任意选择一个目录,点击生成xml,选择存储路径和名称,即可把目录结构存储成xml文件。
当然,具体的xml的节点名称,你可以自定义修改,下面给出源码,源码也参考了网上一些并进行了自适应的修改。
lmainwindow.h
#ifndef LMAINWINDOW_H #define LMAINWINDOW_H #include <QMainWindow> #include<QFileDialog> #include<QDirModel> #include<QTreeWidget> #include<QTreeWidgetItem> #include <QTextStream> #include <QDomElement> #include<QMessageBox> namespace Ui { class lMainWindow; } class lMainWindow : public QMainWindow { Q_OBJECT public: explicit lMainWindow(QWidget *parent = 0); ~lMainWindow(); void LoadFiles(QString path, QTreeWidget *treewidget, QTreeWidgetItem *item); bool SaveXML(QString fileName, QTreeWidget *pTreeWg); void savechilddom(QDomElement root,QTreeWidgetItem* projectItem,QDomDocument doc); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::lMainWindow *ui; }; #endif // LMAINWINDOW_H
lmainwindow.cpp
#include "lmainwindow.h" #include "ui_lmainwindow.h" lMainWindow::lMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::lMainWindow) { ui->setupUi(this); } lMainWindow::~lMainWindow() { delete ui; } void lMainWindow::on_pushButton_clicked() { QString srcDirPath = QFileDialog::getExistingDirectory( this, "选择路径", "D:"); if (srcDirPath.isEmpty()) { return; } ui->lineEdit->setText(srcDirPath); QString pathStr = srcDirPath.right(srcDirPath.size()-srcDirPath.lastIndexOf("/")-1); // QTreeWidgetItem *treeItem1 = new QTreeWidgetItem(ui->treeWidget, QStringList(pathStr)); QTreeWidgetItem *treeItem1 = new QTreeWidgetItem(ui->treeWidget, QStringList(pathStr), 0); //0表示目录 LoadFiles(srcDirPath, ui->treeWidget, treeItem1); } void lMainWindow::LoadFiles(QString path, QTreeWidget *treewidget, QTreeWidgetItem *item) { if (treewidget != NULL) treewidget->addTopLevelItem(item); QDir dir(path); if (!dir.exists()) return; dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoSymLinks); QFileInfoList list = dir.entryInfoList(); int size = list.size(); for (int i = 0; i < size; i++) { QFileInfo info = list.at(i); if (info.fileName() == "." || info.fileName() == "..") continue; if (info.isDir()) { QTreeWidgetItem *fileItem = new QTreeWidgetItem(QStringList() << info.fileName(), 0); //0表示目录 if (treewidget == NULL) item->addChild(fileItem); // else treewidget->addTopLevelItem(fileItem); else item->addChild(fileItem); LoadFiles(info.filePath(), NULL, fileItem); } else { QTreeWidgetItem *fileItem = new QTreeWidgetItem(QStringList() << info.fileName(), 1); //1表示是文件 if (treewidget == NULL) item->addChild(fileItem); // else treewidget->addTopLevelItem(fileItem); else item->addChild(fileItem); } } } void lMainWindow::savechilddom(QDomElement root,QTreeWidgetItem* projectItem,QDomDocument doc) { int countSystem = projectItem->childCount(); for (int i = 0; i < countSystem; i++) { QTreeWidgetItem *systemItem = projectItem->child(i); QString systemName = systemItem->text(0); QDomElement elementSystem = doc.createElement("SystemStr"); QDomAttr elementSystemAttr = doc.createAttribute("NameStr"); elementSystemAttr.setValue(systemName); elementSystem.setAttributeNode(elementSystemAttr); root.appendChild(elementSystem); savechilddom(elementSystem,systemItem,doc); } } bool lMainWindow::SaveXML(QString fileName, QTreeWidget *pTreeWg) { QTreeWidgetItem *rootItem = pTreeWg->invisibleRootItem(); //树的根节点 QString aaa=rootItem->child(0)->text(0); int countSystem = rootItem->childCount(); if (countSystem <= 0) //列表树没有项 { return false; } QFile db(fileName); if (!db.open(QIODevice::WriteOnly | QIODevice::Truncate)) return false; //只写方式打开,并清空以前的信息 QDomDocument doc; QDomProcessingInstruction instruction; instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""); doc.appendChild(instruction); QDomElement root = doc.createElement("Root"); QString projectName ="projectname" ; QDomAttr elementProjAttr = doc.createAttribute("NameStr"); elementProjAttr.setValue(projectName); root.setAttributeNode(elementProjAttr); doc.appendChild(root); //添加根元素 QTreeWidgetItem* projectItem = rootItem->child(0); savechilddom(root,projectItem,doc); QTextStream out(&db); out.setCodec("UTF-8"); doc.save(out, 4); db.close(); return true; } void lMainWindow::on_pushButton_2_clicked() { // 将文件另存为docbyqt.doc,关闭工作文档,退出应用程序 QString filename = QFileDialog::getSaveFileName(this, "导出XML文件", "D:/Output", tr("*.xml;; *.XML")); //选择路径 if (filename.isEmpty()) { return; } bool isok=SaveXML(filename, ui->treeWidget); if(isok!=false) QMessageBox::information(NULL,"提示","成功导出",QMessageBox::Ok); }
有任何疑惑或者问题,请进一步交流学习。