Results 1 to 5 of 5

Thread: how to save a treewidget

  1. #1
    Join Date
    Apr 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to save a treewidget

    Hi,
    Can anyone help on saving a TreeWidget into .csv file. I tried different ways but i am able to save only parent item, i am not able to traverse through children.
    code is given in attachment.
    Thanks in advance
    namitha
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to save a treewidget

    Firstly.... A screen shot of code? Copy and paste is not only easier but more useful.

    Secondly, your code makes no attempt to descend into children records if they exist.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    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 a treewidget

    Typically one traverses a tree-like structure using a recursive function:

    Qt Code:
    1. void saveTreeNode( QTreeWidgetItem * pItem, int indent )
    2. {
    3. if ( pItem == 0 )
    4. return;
    5.  
    6. // Add code here to save the text in the item's columns
    7. // Hint: use pItem->columnCount() to get the number of columns for the current item.
    8. // Iterate over the columns.
    9. // Write each column's text to the same row in the CSV file, separated by commas. Use the "indent" value to insert empty CSV fields
    10. // at the start of the row so you can preserve the tree structure. Or choose some other way to indicate
    11. // in the CSV file what the current level is (like maybe just write the indent value as the first
    12. // field in the CSV row)
    13.  
    14. // Now save children
    15. int nKids = pItem->childCount();
    16. for ( int nKid = 0; nKid < nKids; ++nKid )
    17. saveTreeNode( pItem->child( nKid ), indent + 1 );
    18. }
    To copy to clipboard, switch view to plain text mode 

    You start the whole thing off with a call to:

    Qt Code:
    1. saveTreeNode( pTree->invisibleRootItem(), 0 );
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 10th August 2016 at 05:21.
    <=== 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.

  4. #4
    Join Date
    Apr 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: how to save a treewidget

    Hi,
    I tried but only root is saved everytime. Can you please help me with the code that

    How to Write each column's text to the same row in the CSV file, separated by commas. Use the "indent" value to insert empty CSV fields
    // at the start of the row so you can preserve the tree structure. Or choose some other way to indicate
    // in the CSV file what the current level is (like maybe just write the indent value as the first
    // field in the CSV row).

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to save a treewidget

    Quote Originally Posted by namitha View Post
    I tried but only root is saved everytime. Can you please help me with the code that
    If you post the code that does not work we can likely help you find the problem in it.

    Cheers,
    _

Similar Threads

  1. Save TreeWidget into XML-FIle
    By mayrhofer in forum Newbie
    Replies: 3
    Last Post: 27th December 2015, 13:28
  2. QVariant : save : unable to save type 128
    By capbee in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2013, 09:10
  3. TreeWidget
    By olivier1978 in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2013, 18:48
  4. Replies: 4
    Last Post: 29th May 2012, 23:37
  5. Replies: 2
    Last Post: 18th May 2010, 22:44

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.