Hy everybody, I’ve just started learning a bit of this amazing language
but I’ve encountered some problems.
I have a sqlite3 database wich contains a table and I want to populate a
Gtk::ComboBox with the data on it.
To create the GUI I’ve used Glade and this is a part of my code:
@db.execute(“SELECT cat FROM table ORDER BY cat”) do |row|
puts row
end
Now, the script print the rows correctly, but I can’t manage to store
them in my ComboBox called trough
I have a sqlite3 database wich contains a table and I want to populate a
Gtk::ComboBox with the data on it.
Hi,
if you have:
rows= @db.execute(“SELECT cat FROM table ORDER BY cat”)
that should return an array of rows from your table. Each of these rows
themselves be an array of the fields from that row. So you should be
able to do something like this:
rows.each{|r| @combo.append_text(r[0])}
For documentation on RubyGTK2 you may want to have a look at
[…] it looks like Glade is forcing your hand into
using the more robust(complex?) MVC based TreeModel structure, for which
there is a bit of a learning curve
yes, you’re right. However I managed to run, here the code:
combo_cat = @glade[“combo_categoria”]
list_cat = Gtk::ListStore.new(String, String)
cell_cat = Gtk::CellRendererText.new()
combo_cat.pack_start(cell_cat, true)
combo_cat.add_attribute(cell_cat, ‘text’,0)
combo_cat.set_model(list_cat) @db.execute(“SELECT categoria FROM categorie_clienti ORDER BY
categoria”) do |row|
iter = list_cat.append
iter[0] = row[0]
end
/usr/lib/ruby/1.8/sqlite3/database.rb: line 643
Gtk-CRITICAL **:gtk_combo_box_append_text: assertion
`GTK_IS_LIST_STORE (combo_box->priv->model)’ failed
note that the combo was not created with Gtk::ComboBox.new but retrieved
with @glade[“combo”] … don’t know if it matters
Hmm…The ComboBox documentation would lead me to believe that
.append_text() should work for your needs if you were writing your
script from scratch, but it looks like Glade is forcing your hand into
using the more robust(complex?) MVC based TreeModel structure, for which
there is a bit of a learning curve