Results 1 to 2 of 2

Thread: Convert XML to QTreeView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 08:11.

  2. 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, 19:54
  2. convert to QPixmap
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2011, 04:18
  3. how to convert qml to exe?
    By hashb in forum Qt Programming
    Replies: 19
    Last Post: 7th November 2010, 00:32
  4. How to convert XML+XSL to PDF in QT4.5
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2009, 23: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, 11: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
  •  
Qt is a trademark of The Qt Company.