ListStore - possible to use String and Pixbuf image in the same field?

Hi,

I use something like this:

@list_store_party = Gtk::ListStore.new(
Gdk::Pixbuf,
String, # ID
String, # Name
String, # Profession
String, # Number of Attacks.
String, # Race
String, # Gender
String, # Level
String, # Hitpoints
Gdk::Pixbuf,
Float # Lifebar
)

Now I would like to display both a String and an Image (Gdk::Pixbuf) in
a ListStore field (displayed via a TreeView).

But is this possible?

More specifically, in this display of a “fantasy hero group”, I would
like to display the gender (“male” or “female”) but also show a little
gender icon right next to this text. But, I don’t want to use a new
field.

So my question is, can I combine both String and Gdk::Pixbuf in one
field?

On 20 September 2011 23:53, Marc H.
[email protected] wrote:

More specifically, in this display of a “fantasy hero group”, I would
like to display the gender (“male” or “female”) but also show a little
gender icon right next to this text. But, I don’t want to use a new
field.

So my question is, can I combine both String and Gdk::Pixbuf in one
field?

Yes - https://gist.github.com/1231530

Most of the work is done by the Gtk::TreeViewColumn, which can contain
multiple Gtk::CellRenderers.

Gtk::TreeViewColumn#pack_start(renderer, expand) &
Gtk:TreeViewColumn#set_attributes(renderer, attributes) then allow you
to configure what data it displays from the model (ie. the
Gtk::ListStore).

HTH,

Geoff

Marc H. wrote in post #1023026:

Now I would like to display both a String and an Image (Gdk::Pixbuf) in
a ListStore field (displayed via a TreeView).

But is this possible?

It sure is.

More specifically, in this display of a “fantasy hero group”, I would
like to display the gender (“male” or “female”) but also show a little
gender icon right next to this text. But, I don’t want to use a new
field.

So my question is, can I combine both String and Gdk::Pixbuf in one
field?

The ‘store’ cannot do that, of course, but a treeview column can have
multiple cellrenderers. You have to think at a single cell like an hbox
of cellrenderers.

For example (untested):

column = Gtk::TreeViewColumn.new(“Gender”)

pixbuf_renderer = Gtk.cellRendererPixbuf.new
column.pack_start(pixbuf_renderer, false)
column.set_cell_data_func(pixbuf_renderer) do |column, cell, model,
iter|
if iter[6] ~= /$m/i then
cell.pixbuff = <male_pixbuff>
elsif iter[6] ~= /$f/i then
cell.pixbuff = <female_pixbuff>
else
cell.pixbuff = <unknown_pixbuff>
end
end

gender_text_renderer = Gtk.cellRendererText.new
column.pack_start(gender_text_renderer, false)
column.set_attributes(gender_text_renderer, :text => 6)

You can have a look at the pygtk tutorial :
http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html
mainly
http://www.pygtk.org/pygtk2tutorial/examples/filelisting.py

regards

Simon

It works!

Thanks a lot for your help, Geoff and Simon!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs