Forum: Ruby-Gnome 2 refresh combobox context

Posted by CC Chen (bigdicky)
on 2011-08-18 07:00
How to refresh combobox context?

I want to load a file which it have lots of header on each column,
I put each header into combobox, let someone to select which header
column
does he want to parser.


When I re-select other files I want it refresh the combobox header
context.

I used "clear" but it will cause the combobox empty.
When I re-select other files it also show empty.

How to fix the problem?


Thanks.
Posted by Simon Arnaud (sarnaud)
on 2011-08-18 12:01
CC Chen wrote in post #1017232:
> How to refresh combobox context?

You mean : How to refresh items of a combobox ?

> I used "clear" but it will cause the combobox empty.
> When I re-select other files it also show empty.

Gtk::Combobox#clear affects the combobox itself, not what it displays.
That is, you are clearing the widget of its layout, not its content.

>
> How to fix the problem?

I guess you are using combobox with simple text. The only way is to
iterate on items and remove them one by one. But I urge you not to do
that.

If you want to update the combobox content, use a model. Take a look at
the examples in the sources , gtk2/sample/misc.

Here is a sample for your needs, as I understand them :

require 'gtk2'

model = Gtk::ListStore.new( String)
["foo", "bar", "baz"].each do |text|
  iter = model.append
  iter[0] = text
end

combo = Gtk::ComboBox.new(model)

renderer = Gtk::CellRendererText.new
combo.pack_start(renderer, true)
combo.set_attributes(renderer, :text => 0)

combo.active = 1

combo.signal_connect("changed") do |c|
  p "combo: #{c.active}, #{c.active_iter[0]}" if c.active > -1
end

button = Gtk::Button.new("Click me")
button.signal_connect("clicked") do |button|
  model.clear
  ["ruby", "python", "perl"].each do |text|
    iter = model.append
    iter[0] = text
  end
  combo.active = 0
end

window = Gtk::Window.new
window.signal_connect("destroy") { Gtk.main_quit }
vbox = Gtk::VBox.new
vbox.add(combo).add(button)
window.add(vbox).show_all

Gtk.main


Hope it helps.

Simon
Posted by CC Chen (bigdicky)
on 2011-08-23 06:47
Simon Arnaud wrote in post #1017273:
> CC Chen wrote in post #1017232:

>
> Here is a sample for your needs, as I understand them :
>
> require 'gtk2'
>
> model = Gtk::ListStore.new( String)
> ["foo", "bar", "baz"].each do |text|
>   iter = model.append
>   iter[0] = text
> end
>
> combo = Gtk::ComboBox.new(model)
>
> renderer = Gtk::CellRendererText.new
> combo.pack_start(renderer, true)
> combo.set_attributes(renderer, :text => 0)
>
> combo.active = 1
>
> combo.signal_connect("changed") do |c|
>   p "combo: #{c.active}, #{c.active_iter[0]}" if c.active > -1
> end
>
> button = Gtk::Button.new("Click me")
> button.signal_connect("clicked") do |button|
>   model.clear
>   ["ruby", "python", "perl"].each do |text|
>     iter = model.append
>     iter[0] = text
>   end
>   combo.active = 0
> end
>
> window = Gtk::Window.new
> window.signal_connect("destroy") { Gtk.main_quit }
> vbox = Gtk::VBox.new
> vbox.add(combo).add(button)
> window.add(vbox).show_all
>
> Gtk.main
>
>
> Hope it helps.
>
> Simon



I tried your method, and could refresh combobox content.
You really give me a big hand.


Thanks your help.


CC
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.