ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 13日 (月) 01:16:07 JST
------------------------- REMOTE_ADDR = 184.145.90.35 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview-model-retrieve ------------------------- @@ -5,25 +5,27 @@ 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 - - iter[0] = "Joe" - iter[1] = "Average" - iter[2] = 1970 - - # Append another row and fill in some data - iter = liststore.append - - iter[0] = "Jane" - iter[1] = "Common" - iter[2] = 1967 - - liststore.each do |model,path,iter| - printf("Row %s: %s %s, born %i\n", path.to_str, iter[0], iter[1], iter[2]); - end + # 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; 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.