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

**URL:** <https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059>\
**Category:** Ruby-Gnome 2\
**Created:** [September 21, 2011, 12:53am UTC](https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059 "2011-09-21T00:53:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 21, 2011, 12:53am UTC](https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059/1 "2011-09-21T00:53:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 21, 2011, 10:14am UTC](https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059/2 "2011-09-21T10:14:02Z")

</div>

On 20 September 2011 23:53, Marc H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 - [1231530’s gists · GitHub](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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 21, 2011, 9:50am UTC](https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059/3 "2011-09-21T09:50:46Z")

</div>

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](http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html)  
mainly  
[http://www.pygtk.org/pygtk2tutorial/examples/filelisting.py](http://www.pygtk.org/pygtk2tutorial/examples/filelisting.py)

regards

Simon

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 22, 2011, 9:30pm UTC](https://www.ruby-forum.com/t/liststore-possible-to-use-string-and-pixbuf-image-in-the-same-field/212059/4 "2011-09-22T21:30:04Z")

</div>

It works!

Thanks a lot for your help, Geoff and Simon!
