Sign up

Creating a Tree.

Hello! I'm incredibly confused with what may be something incredibly simple to you, but I'm new to this. I'm trying to create a tree (nodes, parents, children etc...) but I'm reading the documentation and it just confuses me... Also I can't find any examples! Any help is very much appreciated!

Re: Creating a Tree.

On Wed, 31 Jul 2013 02:19:39 GMT, Alan wrote:

Hello! I'm incredibly confused with what may be something incredibly simple to you, but I'm new to this. I'm trying to create a tree (nodes, parents, children etc...) but I'm reading the documentation and it just confuses me... Also I can't find any examples! Any help is very much appreciated!

You probably want to use the gtk.TreeView and a TreeStore.
Here is a small example, Gtk+/GtkD uses an TreeIter to keep track of where the node is appended to the model.

//Create a TreeStore to serve as the model for your tree,
//And create an treeview to display it.
TreeStore store = new TreeStore([GType.STRING]);
TreeView view = new TreeView(store);

//Add an column to the view to display some text.
TreeViewColumn column = new TreeViewColumn("Header Text",new CellRendererText(),"text", 0);
view.appendColumn(column);

//Get an iter to the root of the model/tree.
TreeIter root = store.append(null);

//Add an node to the root.
TreeIter parent = store.append(root);
store.setValue(parent, 0, "Parent 1");

//Add two children to the node created previously.
TreeIter child = store.append(parent);
store.setValue(parent, 0, "Child 1");
child = store.append(parent);
store.setValue(parent, 0, "Child 2");

//Add an other node to the root.
TreeIter parent = store.append(root);
store.setValue(parent, 0, "Parent 2");

TreeIter child = store.append(parent);
store.setValue(parent, 0, "Child 3");
child = store.append(parent);
store.setValue(parent, 0, "Child 4");

This should get you a tree like this:

Parent 1
├─ Child 1
└─ Child 2
Parent 2
├─ Child 3
└─ Child 4

Re: Creating a Tree.

Oh! Thank you so much! I really needed an example and this did it, thanks a lot!