QT5自学教程(72)——xml Editor

前端之家收集整理的这篇文章主要介绍了QT5自学教程(72)——xml Editor前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

QT5自学教程(72)——xml Editor

利用前面两节介绍的读写xml的知识,在这一节实现一个简单xml编辑器。下面给出我们的简单例子。

[1]建立一个gui项目


[2]在窗口中添加一个tree view控件和一个PushButton控件

[3]在项目中添加必要的程序

dialog.h程序

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5. #include <QDebug>
  6. #include <QFile>
  7. #include <QStandardItemModel>
  8. #include <QDomDocument>
  9. namespace Ui {
  10. class Dialog;
  11. }
  12.  
  13. class Dialog : public QDialog
  14. {
  15. Q_OBJECT
  16.  
  17. public:
  18. explicit Dialog(QWidget *parent = 0);
  19. ~Dialog();
  20.  
  21. private slots:
  22. void on_pushButton_clicked();
  23.  
  24. private:
  25. Ui::Dialog *ui;
  26. QStandardItemModel *model;
  27. QString fileName;
  28. void ReadFile();
  29. void WriteFile();
  30. };
  31.  
  32. #endif // DIALOG_H

dialog.cpp程序

  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3.  
  4. Dialog::Dialog(QWidget *parent) :
  5. QDialog(parent),ui(new Ui::Dialog)
  6. {
  7. ui->setupUi(this);
  8. fileName = "F:/test.xml";
  9. model = new QStandardItemModel(0,1,this);
  10. ReadFile();
  11. ui->treeView->setModel(model);
  12. }
  13.  
  14. Dialog::~Dialog()
  15. {
  16. delete ui;
  17. }
  18. //Read XML
  19. void Dialog::ReadFile()
  20. {
  21. QStandardItem *root = new QStandardItem("Books");
  22. model->appendRow(root);
  23. QDomDocument doc;
  24. //Load the xml file
  25. QFile file(fileName);
  26. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
  27. {
  28. doc.setContent(&file);
  29. file.close();
  30. }
  31. //get xml root element
  32. QDomElement xmlRoot = doc.firstChildElement();
  33. //Read the book
  34. QDomNodeList books = xmlRoot.elementsByTagName("Book");
  35. for(int i = 0; i < books.count(); ++i)
  36. {
  37. QDomElement book = books.at(i).toElement();
  38. QStandardItem *bookItem = new QStandardItem(book.attribute("Name"));
  39. //Read the Chapter of the book
  40. QDomNodeList chapters = book.elementsByTagName("Chapter");
  41. for(int h = 0; h < chapters.count(); ++h)
  42. {
  43. QDomElement chapter = chapters.at(h).toElement();
  44. QStandardItem *chapterItem = new QStandardItem(chapter.attribute("Name"));
  45. bookItem->appendRow(chapterItem);
  46. }
  47. root->appendRow(bookItem);
  48. }
  49. }
  50. //Write XML
  51. void Dialog::WriteFile()
  52. {
  53. //Write the xml file
  54. QDomDocument doc;
  55. //Make a root node
  56. QDomElement xmlRoot = doc.createElement("Books");
  57. doc.appendChild(xmlRoot);
  58. QStandardItem *root = model->item(0,0);
  59. for(int i = 0; i < root->rowCount(); ++i)
  60. {
  61. QStandardItem *book = root->child(i,0);
  62. QDomElement xmlBook = doc.createElement("Book");
  63. xmlBook.setAttribute("Name",book->text());
  64. xmlBook.setAttribute("ID",i);
  65. xmlRoot.appendChild(xmlBook);
  66. for(int h = 0; h < book->rowCount(); ++h)
  67. {
  68. QStandardItem *chapter = book->child(h,0);
  69. QDomElement xmlChapter = doc.createElement("Chapter");
  70. xmlChapter.setAttribute("Name",chapter->text());
  71. xmlChapter.setAttribute("ID",i);
  72. xmlBook.appendChild(xmlChapter);
  73. }
  74. }
  75. //Save to disk
  76. QFile file(fileName);
  77. if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
  78. {
  79. qDebug() << "Failed to write file";
  80. }
  81. QTextStream out(&file);
  82. out << doc.toString();
  83. out.flush();
  84. file.close();
  85. qDebug() << "Finished";
  86. }
  87.  
  88. void Dialog::on_pushButton_clicked()
  89. {
  90. //Save
  91. WriteFile();
  92. }

[4]输出结果

(1)原始xml打开后显示

(2)在窗口修改xml相关项之后点击save,关闭应用,再次打开应用后显示如下

小结

这一节实现了一个简单xml编辑器应用程序。PS: 今天浑身酸痛,可能做得事情有点多再加上昨天打了三个小时篮球,效率很低诶。那今天就先到这里好了o(╯□╰)o

2014/9/16

Wayne HDU

猜你在找的XML相关文章