Results 1 to 2 of 2

Thread: Convert XML to QTreeView

  1. #1
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question Convert XML to QTreeView

    Hi all,

    I read a very simple XML created by me. I parse it on a QDomDocument.

    My XML is:
    Qt Code:
    1. <?xml version='1.0' encoding='utf-8'?>
    2. <data>
    3. <feature>
    4. <name>Sex</name>
    5. <value>Male</value>
    6. <value>Female</value>
    7. <value>Unknown</value>
    8. </feature>
    9. <feature>
    10. <name>Color</name>
    11. <value>Red</value>
    12. <value>Green</value>
    13. <value>Blue</value>
    14. <value>White</value>
    15. <value>Black</value>
    16. <value>Unknown</value>
    17. </feature>
    18. <feature>
    19. <name>Size</name>
    20. <value>Small</value>
    21. <value>Medium</value>
    22. <value>Large</value>
    23. <value>Unknown</value>
    24. </feature>
    25.  
    26. </data>
    To copy to clipboard, switch view to plain text mode 

    I parse the XML:
    Qt Code:
    1. QDomDocument doc("mydocument");
    2. QFile file("metadata.xml");
    3. if (!file.open(QIODevice::ReadOnly)){
    4. cout<< "ERROR"<< endl;
    5. return;
    6. }
    7. if (!doc.setContent(&file)) {
    8. cout << "ERROR" << endl;
    9. file.close();
    10. return;
    11. }
    12. file.close();
    To copy to clipboard, switch view to plain text mode 

    So, I want to show the XML in a QTreeView.To do that, I have to create a QStandardItemModel. ¿Any ideas?

    My idea is something like this:
    Qt Code:
    1. void MainWindow::preOrder(QDomNode dom, QStandardItemModel *model, int row){
    2. if(!dom.isNull()){
    3. int nRow = row+1;
    4. string aux = dom.nodeName().toStdString();
    5. string name = "name";
    6. string value = "value";
    7. if(dom.isText()){
    8. aux = dom.parentNode().nodeName().toStdString();
    9. if(strcmp(aux.c_str(),name.c_str())==0){
    10. cout << dom.nodeValue().toStdString() << endl;
    11. //New ROW
    12. SubQStandardItem *item = new SubQStandardItem(dom.nodeValue());
    13. item->setEditable(false);
    14. model->setItem(nRow, item);
    15. }
    16. if(strcmp(aux.c_str(),value.c_str())==0){
    17. //New SubItems
    18. cout << "\t" << dom.nodeValue().toStdString() << endl;
    19. SubQStandardItem *subItem = new SubQStandardItem( dom.nodeValue() );
    20. subItem->setCheckable(true);
    21. subItem->setEditable(false);
    22. }
    23. }else{
    24. preOrder(dom.firstChild(), model, nRow);
    25. preOrder(dom.nextSibling(), model, nRow);
    26. }
    27.  
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    This code (not work...) is based on the pre-order algorithm.

    Please help!!!

    Thanks in advance!!!!
    Last edited by sergio87; 27th September 2011 at 14:51.

  2. The following user says thank you to sergio87 for this useful post:


  3. #2
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Post Re: Convert XML to QTreeView

    Hi all,

    I solved it!

    My solution is very simple and it works well for my xml. The code is:

    Qt Code:
    1. void MainWindow::startTree(){
    2. QDomDocument doc("mydocument");
    3. QFile file("metadata.xml");
    4. if (!file.open(QIODevice::ReadOnly)){
    5. cout<< "ERROR"<< endl;
    6. return;
    7. }
    8. if (!doc.setContent(&file)) {
    9. cout << "ERROR" << endl;
    10. file.close();
    11. return;
    12. }
    13. file.close();
    14. preOrder(doc.firstChild(), model);
    15. view = new QTreeView(centralWidget);
    16. view->setModel(model);
    17. view->show();
    18.  
    19. }
    20.  
    21. void MainWindow::insertFather(QString name){
    22. item = new QStandardItem(name);
    23. item->setEditable(false);
    24. }
    25.  
    26. void MainWindow::insertChildren(QString name){
    27. QStandardItem *subItem = new QStandardItem( name );
    28. subItem->setCheckable(true);
    29. subItem->setEditable(false);
    30. item->appendRow(subItem);
    31. }
    32.  
    33. void MainWindow::setItem(QStandardItemModel *model){
    34. model->setItem(nRow, item);
    35. nRow++;
    36. }
    37.  
    38. void MainWindow::preOrder(QDomNode dom, QStandardItemModel *model){
    39. if(!dom.isNull()){
    40. string aux = dom.nodeName().toStdString();
    41. string name = "name";
    42. string value = "value";
    43. if(dom.isText()){
    44. aux = dom.parentNode().nodeName().toStdString();
    45. if(strcmp(aux.c_str(),name.c_str())==0){
    46. cout << dom.nodeValue().toStdString() << endl;
    47. insertFather(dom.nodeValue());
    48. setItem(model);
    49. }
    50. if(strcmp(aux.c_str(),value.c_str())==0){
    51. cout << "\t" << dom.nodeValue().toStdString() << endl;
    52. insertChildren(dom.nodeValue());
    53. }
    54. }else{
    55. preOrder(dom.firstChild(), model);
    56. preOrder(dom.nextSibling(), model);
    57. }
    58.  
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 

    Where item and nRow are declared at header as private.

    Hope it help you!!

    Regards!
    Last edited by sergio87; 28th September 2011 at 09:11.

  4. The following user says thank you to sergio87 for this useful post:


Similar Threads

  1. Convert Pdf to Txt
    By henriquez0 in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2015, 20:54
  2. convert to QPixmap
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2011, 05:18
  3. how to convert qml to exe?
    By hashb in forum Qt Programming
    Replies: 19
    Last Post: 7th November 2010, 01:32
  4. How to convert XML+XSL to PDF in QT4.5
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2009, 00:14
  5. how to convert a xml + xsl to PDF in Qt4.4.3
    By richardander in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2009, 12:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.