Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Gtk# iterate a specific node of a TreeStore, not the root

The code below gives me Node1 Node2 Node3, when I expected Node21 Node22. What is wrong?

In case you know GTK but not GTK# nor C#, here are the signatures of the methods I used. params means variable arguments, out means that the parameter is used as an output, ref` means pass by reference.

  • public TreeIter AppendValues(params object[] values)
  • public TreeIter AppendValues(TreeIter parent, params object[] values)
  • public bool GetIter(out TreeIter iter, TreePath path)
  • public bool IterChildren(out TreeIter iter)
  • public object GetValue(TreeIter iter, int column)
  • public bool IterNext(ref TreeIter iter)

My code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        var node1 = treestore.AppendValues(false, "Node1");
        var node2 = treestore.AppendValues(false, "Node2");
        var node21 = treestore.AppendValues(node2, false, "Node21");
        var node22 = treestore.AppendValues(node2, false, "Node22");
        var node3 = treestore.AppendValues(false, "Node3");
        
        TreeIter it = new TreeIter();
        treestore.GetIter(out it, new TreePath(new[]{1}));
        treestore.IterChildren(out it);
        do
        {
            var data = treestore.GetValue(it, 1);
            Debug.WriteLine(data);
        } while (treestore.IterNext(ref it));

>Solution :

The issue is that you’re starting the iteration at the second item in the tree store and not at node2. You can fix this by starting the iteration at node2 by using node2 as the parent node when calling IterChildren:

var node1 = treestore.AppendValues(false, "Node1");
var node2 = treestore.AppendValues(false, "Node2");
var node21 = treestore.AppendValues(node2, false, "Node21");
var node22 = treestore.AppendValues(node2, false, "Node22");
var node3 = treestore.AppendValues(false, "Node3");

TreeIter it = new TreeIter();
// Start the iteration at node2
treestore.IterChildren(out it, node2);
do
{
    var data = treestore.GetValue(it, 1);
    Debug.WriteLine(data);
} while (treestore.IterNext(ref it));

This will print Node21 and Node22, as expected. Note that if you want to start the iteration at the first child of node2, you can simply use treestore.GetIterFirst(out it, node2) instead of treestore.IterChildren(out it, node2) in the code above.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading