Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

tut-treeview-model-retrieve

Retrieving Row Data

Storing data is not very useful if it cannot be retrieved again. This is done using Gtk::TreeIter#[], which retrieves the value in the given column from the row the TreeIter points to.

Here is the previous example extended to traverse the list store and print out the data stored. As an extra, we use Gtk::TreeModel#each to traverse the store and retrieve the row number from the Gtk::TreePath passed to us in the code block:

# Create a store for first name, last name, and year born
liststore = Gtk::ListStore.new(String, String, Integer)

# Append an empty row and fill in some data
iter = liststore.append

                      # Utilizes:   # Gtk::TreeModel#get_value(iter, column)
iter[0] = "Joe"       # same as >>> # liststore.get_value(iter, 0)
iter[1] = "Average"   # same as >>> # liststore.get_value(iter, 1)
iter[2] = 1970        # same as >>> # liststore.get_value(iter, 2)

# Append another row and fill in some data
iter = liststore.append

                      # Utilizes:   # Gtk::TreeModel#get_value(iter, column)
iter[0] = "Jane"      # same as >>> # liststore.get_value(iter, 0)
iter[1] = "Common"    # same as >>> # liststore.get_value(iter, 1)
iter[2] = 1967        # same as >>> # liststore.get_value(iter, 2)

liststore.each do |model,path,iter|
  printf("Row %s: %s %s, born %i\n", path.to_str, iter[0], iter[1], iter[2]);
end

One thing worth pointing out: means that we need to free it once we are done with it (oops this is Ruby not C!?); the second is that all fields of a row are set to a default nil value appropriate for the data type in question. A field of type Integer will contain the value 0 until it is set to a new value, and strings and all kind of pointer types will be nil until set to something else. Run the above program with an additional empty row and look at the output to see this in effect.

Last modified:2012/08/13 01:23:09
Keyword(s):
References:[tut-treeview]