GTK+ 2.0 Tree View Tutorial using Ocaml | ||
---|---|---|
Prev | Chapter 3. GTree.models for Data Storage: GTree.list_store and GTree.tree_store | Next |
Storing data is not very useful if it cannot be retrieved again. This is done using get method (gtk_tree_model_get), which takes similar arguments as gtk_list_store_set or gtk_tree_store_set do, only that it takes iter and column as arguments and returns the value of the column. The return value is of the same type as the data stored in the particular model column.
Here is the previous example extended to traverse the list store and print out the data stored. As an extra, we use gtk_tree_model_foreach to traverse the store and retrieve the row number from the GtkTreePath passed to us in the foreach callback function:
let cols = new GTree.column_list let col_first_name = cols#add Gobject.Data.string let col_last_name = cols#add Gobject.Data.string let col_year_born = cols#add Gobject.Data.int let foreach_fun model path iter = let first_name = model#get iter col_first_name in let last_name = model#get iter col_last_name in let year_of_birth = model#get iter col_year_born in let tree_path_str = GTree.Path.to_string path in Printf.printf "Row %s: %s %s, born %u\n" tree_path_str first_name last_name year_of_birth; false (* do not stop walking the store, call us with next row *) let create_and_fill_and_dump_store () = let liststore = GTree.list_store cols in (* Append an empty row to the list store. Iter will point to the new row *) let row = liststore#append () in (* Fill fields with some data *) liststore#set ~row ~column:col_first_name "Joe"; liststore#set ~row ~column:col_last_name "Average"; liststore#set ~row ~column:col_year_born 1970; (* Append another row, and fill in some data *) let row = liststore#append () in liststore#set ~row ~column:col_first_name "Jane"; liststore#set ~row ~column:col_last_name "Common"; liststore#set ~row ~column:col_year_born 1967; (* Append yet another row, and fill it *) let row = liststore#append () in liststore#set ~row ~column:col_first_name "Yo"; liststore#set ~row ~column:col_last_name "Da"; liststore#set ~row ~column:col_year_born 1873; (* Now traverse the list *) liststore#foreach (foreach_func liststore) let main () = create_and_fill_and_dump_store () let _ = Printexc.print main ()