Olivier
12/22/2006 4:20:00 PM
Le vendredi 22 décembre 2006 15:29, Patrick Plattes a écrit :
> Hello,
>
> I have a problem with the TreeView of RubyGTK. Catching the signal works
> very well, but I don't know which row of the list was activated :-(.
> Does anyone know how to figure out which line was activated?
>
> I've written a litte script to show what I mean. At the moment I do a
> 'puts "output"', but how could I print the first column of the selected
> row? (or anything else)
>
> If these kind of questions are too Gtk specific, please tell me and I
> won't bother you with them.
>
> Thanks,
> Patrick
>
>
>
>
> #!/usr/bin/env ruby
>
> require 'gtk2'
>
>
> def create_list(headline, liststore)
> treeview = Gtk::TreeView.new(liststore)
> renderer = Gtk::CellRendererText.new
> view = Gtk::ScrolledWindow.new.add(treeview)
>
> headline.each_index do |index|
> column = Gtk::TreeViewColumn.new(headline[index],
> renderer, :text => index)
> column.resizable = true
> column.clickable = true
> column.sort_column_id = index
> treeview.append_column(column)
> end
>
> return view, treeview, liststore
> end
>
>
>
> Gtk::init
>
> window = Gtk::Window.new
> window.title = "Tattletale"
> window.border_width = 10
> window.set_size_request(600, 400)
> window.add(box = Gtk::VBox.new(false, 0))
>
> view, treeview, liststore = create_list(["Foo", "Bar"],
> Gtk::ListStore.new(String, String))
> box.pack_start(view)
> view.show_all
>
> row = liststore.append
> row[0] = "Hello World"
> row[1] = "I agree with Hello World"
>
> row = liststore.append
> row[0] = "Welcome to Brazil"
> row[1] = "Welcome to Denmark"
>
> treeview.signal_connect("row-activated") do |treeview, path, column|
> puts "output"
> end
>
>
> window.show_all
> Gtk::main
You can do all this with the block arguments given :
- "path" tells you which is the activated row
- "treeview" can be asked for the currently selected Iter
(#selection#selected), then you can do whatever you want with it :)
treeview.signal_connect("row-activated") do |treeview, path, column|
puts path
i = treeview.selection.selected
puts "#{i[0]} => #{i[1]}"
end
--
Olivier