Results 1 to 7 of 7

Thread: How to save the content from a QTreeWidget (tree structure, model) to database

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to save the content from a QTreeWidget (tree structure, model) to database

    Hi

    how can i save and load the items for a QTreeWidget(TreeModel) line by line. My current table looks like this(ListModel):

    Qt Code:
    1. CREATE TABLE tree
    2. (
    3. ID integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    4. entries varchar (50) NOT NULL
    5. );
    To copy to clipboard, switch view to plain text mode 

    and i load it by
    Qt Code:
    1. for (int i=0; i<allEntries.size(); i++)
    2. {
    3. item=new QTreeWidgetItem(ui.treeWidget);
    4. item->setText(0, allEntries.at(i).getName());
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now i want add a field (index) to my database, and store the the current TreeModel-Index within to add rows under a row. How can i do that?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    Iterate over the tree and run an INSERT statement for each item you traverse.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    What is the value(index) for my INSERT statement if i iterate over the tree?

    Qt Code:
    1. ui.treeWidget->selectionModel()->currentIndex().row()
    To copy to clipboard, switch view to plain text mode 
    I think thats not the right value if my model looks like this:


    Should i use nested sets-modell and is that easy to handle with a QTreeWidget?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    You surely can't save a hierarchical model into a database that doesn't provide any means to store information about the relationship between items. Furthermore it seems your "tree" is "flat", based on the code snippet you provided. Anyway, the minimal sql table needed to store hierarchical data consists of three columns -- id of the element, data of the element and id of the parent of the element. Then it's just a matter of recursing into the tree and storing that information in the database.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    Shold i store the id of the element, data of the element and id of the parent of the element into one database field or into three?

    What is the common way to load the tree?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    Quote Originally Posted by whitefurrows View Post
    Shold i store the id of the element, data of the element and id of the parent of the element into one database field or into three?
    If you are asking this question then I suggest you get familiar with the concept or relational databases before trying to use one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save the content from a QTreeWidget (tree structure, model) to database

    Hi,

    sorry for my bad english, but i have understand the concept of relational databases. I have start my first try to create a tree structure. It looks good but only one problem exist. The tree should look like this:


    but that is the result



    from this table



    The tree was build with this function
    Qt Code:
    1. addTree(0);
    2.  
    3. void MyWidget::addTree(const int id)
    4. {
    5. QList<Entries*> folders = db->getEntries(id);
    6. MyTreeWidgetItem* item;
    7.  
    8. foreach(Entries* obj, folders)
    9. {
    10. if(obj->getParentID())
    11. {
    12. item=new MyTreeWidgetItem(treeWidget->currentItem());
    13. }
    14. else
    15. {
    16. item=new MyTreeWidgetItem(treeWidget);
    17. }
    18.  
    19. item->setEntries(obj);
    20. item->setText(0, obj->getName());
    21. item->setIcon(0, QIcon(":/folder.ico") );
    22. treeWidget->setCurrentItem(item);
    23.  
    24. addNodes(obj->getID());
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I think the problem is i set every time the current item to the QTreeWidget. I don't known what the right criteria to do that. Can you help me please?
    Attached Images Attached Images
    Last edited by whitefurrows; 20th May 2011 at 21:49.

Similar Threads

  1. Replies: 4
    Last Post: 22nd March 2011, 17:24
  2. Save the structure as a model in a file
    By valgaba in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2010, 15:13
  3. Replies: 1
    Last Post: 21st November 2009, 08:29
  4. Tree structure
    By ikm in forum Newbie
    Replies: 1
    Last Post: 7th August 2009, 20:19
  5. Create Tree Structure
    By bismitapadhy in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2009, 10:13

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.