# GTK TreeView

**URL:** <https://www.ruby-forum.com/t/gtk-treeview/77303>\
**Category:** Ruby\
**Created:** [November 26, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/gtk-treeview/77303 "2006-11-26T14:16:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [November 26, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/gtk-treeview/77303/1 "2006-11-26T14:16:45Z")

</div>

Hello ML :-),

i’ve started to write a small GUI with GTK. A lot of thinks work fine,  
but I have one problem: I want to have a list - like the email list at  
the usual mail client. Tutorials told me, that I have to use TreeView.

I created an instance of ListStore and added a row. I see the row, and  
I’m able to click on the row (the row change the color) but i don’t see  
any text in the cells. Does anyone have an idea how to fix it?

Thanks and bye,  
Patrick

here is my code:

list\_store = Gtk::ListStore.new(String, String, String)  
iter = list\_store.append

iter[0] = “First”  
iter[1] = “Second”  
iter[2] = “Third”

view = Gtk::TreeView.new(list\_store)  
renderer = Gtk::CellRendererText.new

column = Gtk::TreeViewColumn.new(“Foo”, renderer)  
column.resizable = true  
view.append\_column(column)

column = Gtk::TreeViewColumn.new(“Bar”, renderer)  
column.resizable = true  
view.append\_column(column)

column = Gtk::TreeViewColumn.new(“Mix”, renderer)  
column.resizable = true  
view.append\_column(column)

vbox.pack\_start(view)

view.show\_all

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [November 26, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/gtk-treeview/77303/2 "2006-11-26T15:36:50Z")

</div>

> I created an instance of ListStore and added a row. I see the row, and  
> I’m able to click on the row (the row change the color) but i don’t see  
> any text in the cells. Does anyone have an idea how to fix it?

Try to replace these lines :

> column = Gtk::TreeViewColumn.new(“Foo”, renderer)  
> column = Gtk::TreeViewColumn.new(“Bar”, renderer)  
> column = Gtk::TreeViewColumn.new(“Mix”, renderer)

by these :  
column = Gtk::TreeViewColumn.new(“Foo”, renderer, :text =\> 0)  
column = Gtk::TreeViewColumn.new(“Bar”, renderer, :text =\> 1)  
column = Gtk::TreeViewColumn.new(“Mix”, renderer, :text =\> 2)

This is the easy way to affect the content of a column to the data  
stored in  
the model. The last argument of the TreeViewColumn constructor is a hash  
of  
attributes which define a mapping between the data in the model, and  
what  
will appear in the TreeView (those attributes can be reaffected later  
with  
#add\_attribute or #set\_attributes)

In your case, you created this model :

> iter[0] = “First”  
> iter[1] = “Second”  
> iter[2] = “Third”

The attribute {:text =\> 0} means “I want to map directly the content of  
iter[0] to the rendered text of the “Foo” column”.

Another way to do this is by using the #set\_cell\_data\_func method, which  
let  
you define the rendering of the cell from the given block (using  
CellRenderer#text=, for example)

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [November 27, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/gtk-treeview/77303/3 "2006-11-27T23:09:01Z")

</div>

Hello,

thanks for reply. Your code works fine. I’m also able so sort the  
columns :-).

Thanks,  
Patrick
