Results 1 to 4 of 4

Thread: parse items of QTreeWidget into file/QSettings

  1. #1
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default parse items of QTreeWidget into file/QSettings

    Good morning everyone

    does anyone know how to parse the items of a QTreeWidget into a file?
    Im trying to save the list of items in QTreeWIdget with QSettigns but currentItem()->text(0);
    works only for one item

    Iv tried to do : selectAll(); and then currentItem()->text(0) but it freezes my program..

    Some samples Ive tried

    Trying to parse the items on QSettings value
    Qt Code:
    1. treeWidget->selectAll();
    2. settings.setValue("repositoriesLastState",treeWidget->currentItem()->text(0));
    To copy to clipboard, switch view to plain text mode 
    the above on freezes my program, segmentation fault....

    Trying to parse the items on a file for futher processing later.

    Qt Code:
    1. QFile repos("/tmp/repositories");
    2. repos.open(QFile::WriteOnly | QFile::Truncate);
    3. QTextStream repoList(&repos);
    4. repoList << treeWidget->currentItem()->text(0);
    5.  
    6. repos.close();
    To copy to clipboard, switch view to plain text mode 
    the above write to /tmp/repositories file only one item

    Please help, thank you

  2. #2
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: parse items of QTreeWidget into file/QSettings

    ive managed to do the following:

    Qt Code:
    1. //where "repoList" is my treeWidget
    2.  
    3. QFile repositories("/tmp/repositories");
    4. repositories.open(QFile::WriteOnly | QFile::Truncate);
    5. QTextStream repositoryList(&repositories);
    6.  
    7. int count =
    8. parent ? parent->childCount() : repoList->topLevelItemCount();
    9.  
    10. for (int i = 0; i < count; i++)
    11. {
    12. parent ? parent->child(i) : repoList->topLevelItem(i);
    13. // do something with the item
    14. repositoryList << repoList->topLevelItem(i)->text(0);
    15. }
    16. repositories.close();
    To copy to clipboard, switch view to plain text mode 


    I found the above code in an old thread of qt-interest
    The thing is, that even if the above code works nicely, saves my items in one line.

    for example, my items in the QTreeWidget is like this:
    Qt Code:
    1. test repo1
    2. test repo2
    3. test repo3
    To copy to clipboard, switch view to plain text mode 
    The above code saves it like this:
    Qt Code:
    1. test repo 1test repo 2test repo 3
    To copy to clipboard, switch view to plain text mode 

    Ive tried to customize the code but no work...
    any suggestions?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: parse items of QTreeWidget into file/QSettings

    Well if you want linebreaks use:
    Qt Code:
    1. repositoryList << "\n";
    To copy to clipboard, switch view to plain text mode 

    But you should consider using a XML or any other structured file (e.g. csv) to save your items...

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

    Mystical Groovy (8th November 2009)

  5. #4
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: parse items of QTreeWidget into file/QSettings

    ok now i feel stupid...
    thank you

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 21:58
  2. Replies: 0
    Last Post: 18th September 2009, 14:10
  3. QTreeWidget Infinity Child items
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 13:19
  4. amount of items in QTreeWidget
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 22:38
  5. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20

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.