Results 1 to 10 of 10

Thread: C++ Tree View operation

  1. #1
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default C++ Tree View operation

    hi guy ... can you help me to create a struct or give some example to do :

    Original
    Qt Code:
    1. +item1
    2. +item2
    3. -item3
    4. +child1_item
    5. +child2_item
    6. -child3_item
    7. item_c31
    8. item_c32
    9. item_c33
    10. -item4
    11. +child1_item
    12. +child2_item
    13. -child3_item
    14. item_c31
    15. +item5
    To copy to clipboard, switch view to plain text mode 
    if i remove item4->child3_item->item2 struct 'll be
    Qt Code:
    1. +item1
    2. +item2
    3. -item3
    4. +child1_item
    5. +child2_item
    6. -child3_item
    7. item_c31
    8. itemc_33
    9. -item4
    10. +child1_item
    11. +child2_item
    12. -child3_item
    13. item_c31
    14. +item5
    To copy to clipboard, switch view to plain text mode 
    if i remove item4->child3_item struct 'll be

    Qt Code:
    1. +item1
    2. +item2
    3. -item3
    4. +child1_item
    5. +child2_item
    6. -child3_item
    7. item_c31
    8. item_c32
    9. item_c33
    10. -item4
    11. +child1_item
    12. +child2_item
    13. +item5
    To copy to clipboard, switch view to plain text mode 


    if i add item
    Qt Code:
    1. +item1
    2. +item2
    3. -item3
    4. +child1_item
    5. +child2_item
    6. -child3_item
    7. item1
    8. item2
    9. item3
    10. -item4
    11. +child1_item
    12. +child2_item
    13. -child3_item
    14. item_c31
    15. +item5
    16. +item6
    To copy to clipboard, switch view to plain text mode 
    and other operation as add child_item ot add item_c, remove . . .

    this is very importa for me please help me i don't have any idea to do this... ;(

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Tree View operation

    if you are looking for a widget you should have a look at QTreeWidget
    if what you want is a data structure (but not a view to display it). You'll have to create it in a very classical way :

    Qt Code:
    1. class TreeNode
    2. {
    3. public:
    4. // methods here
    5. private:
    6. TreeNode *m_parent;
    7. QList<TreeNode*> m_children;
    8. };
    To copy to clipboard, switch view to plain text mode 

    managing the hierarchy of such a structure is quite straightforward but you need to be careful about memory leaks (yeah, the node class MUST be allocated dynamically and used through pointers, there is no alternative).
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    zeeb100 (17th February 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: C++ Tree View operation

    thank for reply .. but i show... there are problem to show this ????

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Tree View operation

    Sorry I really don't understand what you mean...

    If you just want a ready-to-use tree structure that you can display in a couple of lines than QTreeWidget is definitely the right choice.

    If you want to create your own low-level tree structure you can use the skeleton I showed you. To display that you will have to create a custom model and set it to a QTreeView. The tree model example is a pretty good start for that.
    Current Qt projects : QCodeEdit, RotiDeCode

  6. The following user says thank you to fullmetalcoder for this useful post:

    zeeb100 (17th February 2009)

  7. #5
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: C++ Tree View operation

    fullmetalcoder oyu are my hero !!


    the example is perfect. on that basis I would like to add a node, delete a parent node with all children, add a child to a father. I can do it right?

  8. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Tree View operation

    Quote Originally Posted by zeeb100 View Post
    fullmetalcoder oyu are my hero !!
    My pleasure. If my answer did spare you some headbanging and hours of frustration consider using that shiny "thanks" button below the posts

    Quote Originally Posted by zeeb100 View Post
    on that basis I would like to add a node, delete a parent node with all children, add a child to a father. I can do it right?
    Sure you can. Once you have added proper methods to your node class adding/removing children is child's play. e.g :

    Qt Code:
    1. class TreeNode
    2. {
    3. public:
    4. ~TreeNode()
    5. {
    6. qDeleteAll(m_children);
    7. }
    8. void appendChild(TreeNode *c)
    9. {
    10. m_children.append(c);
    11. }
    12. void removeChild(int row)
    13. {
    14. m_children.removeAt(row);
    15. }
    16. void deleteChildRecursively(int row)
    17. {
    18. TreeNode *c = m_children.takeAt(row);
    19. delete c;
    20. }
    21. private:
    22. TreeNode *m_parent;
    23. QList<TreeNode*> m_children;
    24. };
    To copy to clipboard, switch view to plain text mode 

    Please not that the above code is just a skeleton which won't work as is (it does not deal with model/view updating for instance, which is described in the example I refered to in my previous post).
    Current Qt projects : QCodeEdit, RotiDeCode

  9. The following user says thank you to fullmetalcoder for this useful post:

    zeeb100 (17th February 2009)

  10. #7
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: C++ Tree View operation

    it's really hard :s

  11. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Tree View operation

    Yeah, model/view is a bit hard to master but hopefully the tutorials are quite good.

    If you need more example of custom tree model you can have a look at those I have created for use in Edyuk : https://edyuk.svn.sf.net/svnroot/edy...projectmodel2/ and https://edyuk.svn.sf.net/svnroot/edy...y/qcodemodel2/

    They are not very documented but the code is clean enough to serve as an example.
    Current Qt projects : QCodeEdit, RotiDeCode

  12. #9
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Unhappy Re: C++ Tree View operation

    now i use the struct of exemple tree view ok ... but i don't do the this function

    Qt Code:
    1. class TreeNode
    2. {
    3. public:
    4. ~TreeNode()
    5. {
    6. qDeleteAll(m_children);
    7. }
    8. void appendChild(TreeNode *c)
    9. {
    10. m_children.append(c);
    11. }
    12. void removeChild(int row)
    13. {
    14. m_children.removeAt(row);
    15. }
    16. void deleteChildRecursively(int row)
    17. {
    18. TreeNode *c = m_children.takeAt(row);
    19. delete c;
    20. }
    21. private:
    22. TreeNode *m_parent;
    23. QList<TreeNode*> m_children;
    24. };
    To copy to clipboard, switch view to plain text mode 

    how can access item of model ???

    example

    insert a child in the parent specify
    Qt Code:
    1. parent_z
    2. child1
    3. child2
    4.  
    5. ......
    6. insert(child3) into parent_z
    7. ......
    8. parent_z
    9. child1
    10. child2
    11. child3
    To copy to clipboard, switch view to plain text mode 
    Last edited by zeeb100; 17th February 2009 at 17:55.

  13. #10
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: C++ Tree View operation

    i ok there are successful. but the function ~TreeItem (); it does not eliminate the node because if I execute row count the same value returns always


    i use the The tree model example

Similar Threads

  1. Radio buttons in a tree view
    By notwithstanding in forum Qt Programming
    Replies: 6
    Last Post: 3rd November 2008, 23:32
  2. Tree View with Icons/primitives
    By dosto.walla in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2008, 11:29
  3. Replies: 2
    Last Post: 18th March 2008, 16:38
  4. Ignore mouse events out of tree view
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 12:24
  5. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 09:50

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.