Results 1 to 16 of 16

Thread: How to save QTreeWidget items in an XML file?

  1. #1
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Question How to save QTreeWidget items in an XML file?

    Hi,

    I want to save all QTreeView items in an XML file, including child an parent, I searched but I found nothing... It is possible like below?

    Qt Code:
    1. <root>
    2. <Folder Name="myFolder">
    3. <File Name="myFile" />
    4. </Folder>
    5. <Folder Name="mySecondFolder">
    6. <File Name="myFile2" />
    7. <File Name="myFile3" />
    8. <File Name="myFile4" />
    9. </Folder>
    10. </root>
    To copy to clipboard, switch view to plain text mode 

    QTreeView.png

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    It is possible like below?
    Yes, of course it is possible, but you have to write the code yourself to do it. Look at QDomDocument and the related classes in the Qt XML library.

    Code to do this would perform a depth-first traversal of the QTreeWidget's items. If the item has children, then it is a folder and you create a folder XML element. You then look at each of the children of this item in turn. If the child has no children, then you create a file XML element, set whatever attributes you want, and add it to the folder element you just created. If the child itself has children, then you create another folder element and examine the children of this child. Each time you come to either a file element or the end of the children, you add the element to the current parent element and move on.

    Your code will basically be a top-level function to create the XML document and root element, then a recursive function that takes the current tree item pointer and current QDomElement reference and examines each child of the tree item. If the tree item has children, then create a folder element and for each child, recurse by calling the same function with the new tree item and folder element. When you reach the end of the children, add the folder to its parent and exit the recursive function. The execution will then pick up with the next child and so forth until the entire tree has been traversed.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Thanks!

    I have another question, I do that to save QTreeWidget items in XML file:

    Qt Code:
    1. for (int top=0; top<ui->Tree->topLevelItemCount(); top++)
    2. {
    3. QTreeWidgetItem *item = ui->Tree->topLevelItem(top);
    4.  
    5. QDomElement folder = doc.createElement("Folder");
    6. folder.setAttribute("Name", item->text(0));
    7. root.appendChild(folder);
    8.  
    9. if (item->childCount() > 0)
    10. {
    11. for (int child1=0; child1<item->childCount(); child1++)
    12. {
    13. QTreeWidgetItem *item_child = item->child(child1);
    14.  
    15. QDomElement child_folder = doc.createElement("Folder");
    16. child_folder.setAttribute("Name", item_child->text(0));
    17. folder.appendChild(child_folder);
    18.  
    19. if (item_child->childCount() > 0)
    20. {
    21. for (int child2=0; child2<item_child->childCount(); child2++)
    22. {
    23. QTreeWidgetItem *item_child2 = item_child->child(child2);
    24.  
    25. QDomElement child_folder2 = doc.createElement("Folder");
    26. child_folder2.setAttribute("Name", item_child2->text(0));
    27. child_folder.appendChild(child_folder2);
    28.  
    29. if (item_child2->childCount() > 0)
    30. {
    31. for (int child3=0; child3<item_child2->childCount(); child3++)
    32. {
    33. QTreeWidgetItem *item_child3 = item_child2->child(child3);
    34.  
    35. QDomElement child_folder3 = doc.createElement("Folder");
    36. child_folder3.setAttribute("Name", item_child3->text(0));
    37. child_folder2.appendChild(child_folder3);
    38. }
    39. }
    40. }
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    But how can I create a loop. The code will save items at limit of 4 child, but the code is already too long and I don't want to create limitation of items creation...

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Your code will basically be a top-level function to create the XML document and root element, then a recursive function that takes the current tree item pointer and current QDomElement reference and examines each child of the tree item.
    Do you know what "recursive function" means? If you do, then think about how you would take your for() loop over the children and use a recursive function inside it. If you don't, then haul out your C++ textbook and read about it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Thank for the reply. I do not know what is a recursive function but I will look at that!

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    A recursive function is a function that calls itself. It requires some "stopping condition" to avoid infinite recursion - at some point, the function has to return without calling itself again.

    Your recursive function should look something like this (untested code):

    Qt Code:
    1. /* This code assumes the output XML will look something like this:
    2. <rootNode> (root of the tree)
    3.   <childNode name="abc"> (node with children) (top-level node in the tree)
    4.   <childNode name="def"/> (node with no children)
    5.   <childNode name="hij"> (node with children)
    6.   <childNode name="klm" /> (node with no children)
    7.   </childNode>
    8.   </childNode>
    9.   ... (next top-level node, etc.)
    10. </rootNode>
    11.  
    12. */
    13.  
    14. QDomNode outputChildrenOfNode( QTreeWidgetItem * parentTreeNode, QDomDocument & doc )
    15. {
    16. QDomElement parentDocNode; // gets initialized as a "null" DOM node
    17. if ( parentTreeNode != 0 )
    18. {
    19. parentDocNode = doc.createElement( "childNode" );
    20. QDomAttr nameAttr = doc.createAttribute( "name" );
    21. nameAttr.setValue( parentTreeNode->text( 0 ) );
    22. parentDocNode.appendChild( nameAttr );
    23.  
    24. if ( parentTreeNode->childCount() > 0 ) // Stopping condition: childCount == 0
    25. {
    26. int nChildren = parentTreeNode->childCount();
    27. for ( in nChild = 0; nChild < nChildren; ++nChild )
    28. {
    29. QTreeWidgetItem * childTreeNode = parentTreeNode->child( 0 );
    30. QDomNode childDocNode = outputChildrenOfNode( childTreeNode, doc );
    31. if ( !childDocNode.isNull() )
    32. parentDocNode.appendChild( childDocNode );
    33. }
    34. }
    35. )
    36. return parentDocNode;
    37. }
    To copy to clipboard, switch view to plain text mode 

    You start it off in your top-level function (code not shown) by creating the "rootNode" QDomNode. Then in a loop, you call outputChildrenOfNode() with the top-level QTreeWidgetItem nodes of the tree. After each call to this method, you append the child DOM node to the root DOM node.

    If your tree items have more than one column, you'll add more attributes (with different attribute names, of course) and add them to the QDomNode created in line 19.
    Last edited by d_stranz; 21st November 2017 at 05:15.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Thanks for the code. For launch the function for first time I just add it with the first top Level Item of the list at the Parent Tree Node argument?

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    For launch the function for first time I just add it with the first top Level Item of the list at the Parent Tree Node argument?
    No, you have to loop over all top-level items and add each child DOM node to the root DOM node - basically lines 1-3 of your first post above:

    Qt Code:
    1. rootNode = document.createElement( "rootNode" );
    2. for each top-level item
    3. childNode = outputChildrenOfNode ( top-level node[ i ] )
    4. rootNode.appendChild( childNode )
    To copy to clipboard, switch view to plain text mode 

    If the QTreeView had the concept of a "root node" you could just call it with that, but it does not have that concept. Instead, it has a set of "top-level nodes" and you therefore have to loop over those and add each result to the DOM root node.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    I have do this:

    Qt Code:
    1. QDomNode childNode;
    2. for (int i=0; i<ui->Tree->topLevelItemCount(); i++)
    3. {
    4. childNode = outputChildrenOfNode(ui->Tree->topLevelItem(i), doc);
    5. root.appendChild(childNode);
    6. }
    To copy to clipboard, switch view to plain text mode 

    But with this list:
    List.png

    And in the XML file it return:
    Qt Code:
    1. <Child Name="Folder1">
    2. <Child Name="File1"/>
    3. <Child Name="File1"/>
    4. <Child Name="File1"/>
    5. </Child>
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Just to comment on the recursive function mentioned above:
    In general it is NOT advised to use recusiveness .
    It has several disadvantages, which can be very serious problem depending on the code base.
    Generally it is advised to use a stack instead.
    (This is not to say recursion has no place at all, but for most cases you get only drawbacks and no benefits from recursion)
    Last edited by high_flyer; 22nd November 2017 at 22:01.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    In general it is NOT advised to use excursiveness .
    In general I agree. Recursive traversals of data structures of arbitrary depth could eventually cause problems in scaling. In this case, the OP is serializing a relatively shallow data structure for which the depth of recursion will never be too great, and a recursive method is simple and straightforward.

    @MathFurious: As I said, the code I posted was written on the fly and untested, so there could be bugs. And in fact, in line 29, there -is- a bug - it should read "child( nChild )", not "child( 0 )".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. #12
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Everything works perfectly, thanks!

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    By the way, if you want the XML to look like the example in your first post, all you need to do is to check to see if the current tree node has children. If it does, you make the element name "Folder"; if not, you make it "File" (instead of "Child" in all cases).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #14
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Yes, I have do with icons but thank for the tip.

    By the way, I want the inverse now, get XML elements to a QTreeWidget items. I create a new thread?
    Last edited by MathFurious; 23rd November 2017 at 17:53.

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    I create a new thread?
    Before you do that, why don't you study this example and try to write your own code based on the example? The code in the example's "XbelReader" class does almost exactly what you need to do.

    After you have tried to implement something based on this, if you run into problems you can't solve (using the docs, the debugger, or whatever) then post a question in a new thread, but not before. We're here to help you with code you've written, but not to write it for you.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. #16
    Join Date
    Sep 2017
    Posts
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to save QTreeWidget items in an XML file?

    Yes, it's true. I'll see if I can implement a function that read an XML file. Thank you.

Similar Threads

  1. Replies: 5
    Last Post: 3rd January 2011, 11:15
  2. How to save QGraphicsScene Items to a file?
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 28th February 2010, 10:16
  3. parse items of QTreeWidget into file/QSettings
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2009, 16:52
  4. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03
  5. Save & Restore a selection in QTreeWidget
    By youkai in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2008, 19:54

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.