PDA

View Full Version : C++ Tree View operation



zeeb100
16th February 2009, 22:53
hi guy ... can you help me to create a struct or give some example to do :

Original

+item1
+item2
-item3
+child1_item
+child2_item
-child3_item
item_c31
item_c32
item_c33
-item4
+child1_item
+child2_item
-child3_item
item_c31
+item5

if i remove item4->child3_item->item2 struct 'll be


+item1
+item2
-item3
+child1_item
+child2_item
-child3_item
item_c31
itemc_33
-item4
+child1_item
+child2_item
-child3_item
item_c31
+item5
if i remove item4->child3_item struct 'll be


+item1
+item2
-item3
+child1_item
+child2_item
-child3_item
item_c31
item_c32
item_c33
-item4
+child1_item
+child2_item
+item5


if i add item

+item1
+item2
-item3
+child1_item
+child2_item
-child3_item
item1
item2
item3
-item4
+child1_item
+child2_item
-child3_item
item_c31
+item5
+item6
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... ;(

fullmetalcoder
16th February 2009, 23:02
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 :


class TreeNode
{
public:
// methods here
private:
TreeNode *m_parent;
QList<TreeNode*> m_children;
};

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).

zeeb100
16th February 2009, 23:15
thank for reply .. but i show... there are problem to show this ????

fullmetalcoder
16th February 2009, 23:29
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 (http://doc.trolltech.com/4.4/itemviews-simpletreemodel.html) is a pretty good start for that.

zeeb100
16th February 2009, 23:34
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?

fullmetalcoder
17th February 2009, 00:08
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 ;)


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 :



class TreeNode
{
public:
~TreeNode()
{
qDeleteAll(m_children);
}
void appendChild(TreeNode *c)
{
m_children.append(c);
}
void removeChild(int row)
{
m_children.removeAt(row);
}
void deleteChildRecursively(int row)
{
TreeNode *c = m_children.takeAt(row);
delete c;
}
private:
TreeNode *m_parent;
QList<TreeNode*> m_children;
};

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).

zeeb100
17th February 2009, 12:20
:eek::( it's really hard :s

fullmetalcoder
17th February 2009, 13:15
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/edyuk/trunk/3rdparty/qprojectmodel2/ and https://edyuk.svn.sf.net/svnroot/edyuk/trunk/3rdparty/qcodemodel2/

They are not very documented but the code is clean enough to serve as an example.

zeeb100
17th February 2009, 16:12
now i use the struct of exemple tree view ok ... but i don't do the this function



class TreeNode
{
public:
~TreeNode()
{
qDeleteAll(m_children);
}
void appendChild(TreeNode *c)
{
m_children.append(c);
}
void removeChild(int row)
{
m_children.removeAt(row);
}
void deleteChildRecursively(int row)
{
TreeNode *c = m_children.takeAt(row);
delete c;
}
private:
TreeNode *m_parent;
QList<TreeNode*> m_children;
};


how can access item of model ???

example

insert a child in the parent specify


parent_z
child1
child2

......
insert(child3) into parent_z
......
parent_z
child1
child2
child3

zeeb100
18th February 2009, 18:09
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