# Selection in TreeView

**URL:** <https://www.ruby-forum.com/t/selection-in-treeview/106175>\
**Category:** Ruby-Gnome 2\
**Created:** [June 7, 2007, 3:56am UTC](https://www.ruby-forum.com/t/selection-in-treeview/106175 "2007-06-07T03:56:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [June 7, 2007, 3:56am UTC](https://www.ruby-forum.com/t/selection-in-treeview/106175/1 "2007-06-07T03:56:10Z")

</div>

I have several columns in a row. The following code, which I’ve  
borrowed from another post and modified, selects the row correctly, but  
I, a Ruby newbie, don’t know how to query which row the user clicked on.  
The line  
puts "Selection was: "+widget.selection.selected[0]  
outputs the value of column 0 in this case, but I don’t want to specify  
the column. I want to know which column, as well as which row, that the  
user clicked on. Thanks for any help.

#!/usr/bin/ruby  
require ‘gtk2’  
Gtk.init

# SimpleTreeView.new

class SimpleTreeView

# === initialize

def initialize  
@main\_window = Gtk::Window.new  
@main\_window.set\_size\_request 555,255  
create\_parts  
end

def create\_parts  
@renderer=Gtk::CellRendererText.new  
@renderer.set\_property( ‘background’, ‘lavender’ )  
@renderer.set\_property( ‘foreground’, ‘black’ )

```
@list_store = Gtk::ListStore.new(String,String)
@tree_view = Gtk::TreeView.new(@list_store)
@tree_view_col1 = Gtk::TreeViewColumn.new('Col 1', @renderer, {:text

```

=\> 0})  
@tree\_view.append\_column(@tree\_view\_col1)  
@tree\_view\_col2 = Gtk::TreeViewColumn.new(‘Col 2’, @renderer, {:text  
=\> 1})  
@tree\_view.append\_column(@tree\_view\_col2)

```
# Example Data:
iter = @list_store.append
iter.set_value(0, 'a0')
iter.set_value(1, 'a1')
iter = @list_store.append
iter.set_value(0, 'b0')
iter.set_value(1, 'b1')
iter = @list_store.append
iter.set_value(0, 'c0')
iter.set_value(1, 'c1')

@main_window.signal_connect("destroy") { Gtk.main_quit }

@tree_view.signal_connect('button_release_event') do |widget,event|
  puts "Selection was: "+widget.selection.selected[0]
end

@main_window.add(@tree_view)
@main_window.show_all
Gtk.main

```

end  
end  
SimpleTreeView.new

---

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [June 7, 2007, 4:17am UTC](https://www.ruby-forum.com/t/selection-in-treeview/106175/2 "2007-06-07T04:17:06Z")

</div>

Alan L. wrote:

> I have several columns in a row. The following code, which I’ve  
> borrowed from another post and modified, selects the row correctly, but  
> I, a Ruby newbie, don’t know how to query which row the user clicked on.  
> snip  
> I said the opposite of what I meant. As this code shows, I know how to  
> get the selected row. What I don’t know (and would like to learn) is  
> how to determine which _column_ within that row the user has clicked on.  
> Thanks for the help.

---

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [June 7, 2007, 9:38am UTC](https://www.ruby-forum.com/t/selection-in-treeview/106175/3 "2007-06-07T09:38:36Z")

</div>

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@alake](https://www.ruby-forum.com/u/alake)\
**Post date:** [June 7, 2007, 9:45am UTC](https://www.ruby-forum.com/t/selection-in-treeview/106175/4 "2007-06-07T09:45:37Z")

</div>

Am Donnerstag, den 07.06.2007, 04:17 +0200 schrieb Alan L.:

> Alan L. wrote:
> 
> > I have several columns in a row. The following code, which I’ve  
> > borrowed from another post and modified, selects the row correctly, but  
> > I, a Ruby newbie, don’t know how to query which row the user clicked on.  
> > snip  
> > I said the opposite of what I meant. As this code shows, I know how to  
> > get the selected row. What I don’t know (and would like to learn) is  
> > how to determine which _column_ within that row the user has clicked on.  
> > Thanks for the help.

Hi,

i don’t know about a direct way, but if you replace your button-release  
handler with:

@tree\_view.signal\_connect(‘button\_release\_event’) do |widget,event|  
path, column, cell\_x, cell\_y = @tree\_view.get\_path\_at\_pos(event.x,  
event.y)  
p @tree\_view.columns.index(column)  
end

you get what you want. What does it? First it gets the TreeView::Column  
at you button-release event. Than an array with all columns of the  
treeview is requested and there is the column looked up.

Cheers  
detlef

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
