# Cell background of a liststore treeview?

**URL:** <https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755>\
**Category:** Ruby-Gnome 2\
**Created:** [November 24, 2012, 8:09pm UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755 "2012-11-24T20:09:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 24, 2012, 8:09pm UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755/1 "2012-11-24T20:09:12Z")

</div>

Hi,

I need a treeview expert to set a background color of a particular  
cell of a liststore,  
here the code i extracted from my project.  
The goal is to set only the cell corresponding to severity “high” to  
background color red, as the data are dynamically added to the  
liststore using the append method which is using the same cell  
renderer all the column is set to red ;(

Any idea ?

thanks,  
david

#######################################################################################

#!/usr/bin/ruby

require ‘gtk2’  
require ‘gnomecanvas2’

Gtk.init  
w = Gtk::Window.new

ls = Gtk::ListStore.new(Integer, String, Integer, String, String)  
treeview = Gtk::TreeView.new(ls)  
w.add treeview

column1 = Gtk::TreeViewColumn.new(“Selected”,  
Gtk::CellRendererToggle.new, {:active =\> 0})  
column2 = Gtk::TreeViewColumn.new(“Protocol”,  
Gtk::CellRendererText.new, {:text =\> 1})  
column3 = Gtk::TreeViewColumn.new(“Port”, Gtk::CellRendererText.new,  
{:text =\> 2})  
column4 = Gtk::TreeViewColumn.new(“Service”,  
Gtk::CellRendererText.new, {:text =\> 3})  
column5 = Gtk::TreeViewColumn.new(“Severity”,  
Gtk::CellRendererText.new, {:text =\> 4})

treeview.append\_column(column1)  
treeview.append\_column(column2)  
treeview.append\_column(column3)  
treeview.append\_column(column4)  
treeview.append\_column(column5)  
treeview.show

array = [[1, “tcp”,80,“http”,“high”], [1, “udp”,53,“dns”,“medium”]]  
array.each {|cb, proto, port, service, sev|  
iter = treeview.model.append  
iter[0] = cb  
iter[1] = proto  
iter[2] = port  
iter[3] = service  
iter[4] = sev  
if sev == “high”  
sev\_column = treeview.get\_column(4)  
cells = sev\_column.cell\_renderers  
puts cells  
if cells.size \> 0  
sev\_column.cell\_renderers[0].set\_cell\_background\_gdk(Gdk::Color.new(64515,  
0, 0))  
end  
end  
}

w.show\_all  
Gtk.main

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [November 25, 2012, 12:39am UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755/2 "2012-11-25T00:39:10Z")

</div>

Hi:

The method you need to use is:

Gtk::TreeViewColumn#set\_cell\_data\_func

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeViewColumn#set\_cell\_data\_func](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeViewColumn#set_cell_data_func)

However, there’s a much easier way to accomplish this using visualruby.  
Using visualruby’s listview object, you can load any type of object into  
a listview, then define a method called “visual\_attributes” to define  
how you want the object to appear:

class Server  
def initialize(cb, proto, port)  
@cb = cb  
@proto = proto  
@port = port  
end

def visual\_attributes  
hash = { :text =\> @cb.to\_s }  
hash[:background] = (@port \< 0) ? “red” : “white”  
return hash  
end  
end

Later you create a listview like this:

@list\_view = VR::Listview.new(:cb =\> String, :serv =\> Server)

The Server objects will all appear with the text set to @cb,  
and the background will be red for any port that’s less than zero.

For more look at the Listview Tutorials on [visualruby.net](http://visualruby.net).

Yours,  
Eric C

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [November 25, 2012, 1:05am UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755/3 "2012-11-25T01:05:54Z")

</div>

On Sun, Nov 25, 2012 at 12:39 AM, Eric C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Using visualruby’s listview object, you can load any type of object into  
> def visual\_attributes  
> The Server objects will all appear with the text set to @cb,  
> and the background will be red for any port that’s less than zero.
> 
> For more look at the Listview Tutorials on [visualruby.net](http://visualruby.net).

Thx Eric, i will have a look and try.

regards,  
david

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [November 25, 2012, 3:14am UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755/4 "2012-11-25T03:14:51Z")

</div>

Hi David:

Look at the example program, “active\_record2” it has cells with  
different colors in the same column.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [November 25, 2012, 12:58pm UTC](https://www.ruby-forum.com/t/cell-background-of-a-liststore-treeview/224755/5 "2012-11-25T12:58:30Z")

</div>

On Sun, Nov 25, 2012 at 3:14 AM, Eric C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi David:
> 
> Look at the example program, “active\_record2” it has cells with  
> different colors in the same column.

Thx Eric, i made it works.

regards,  
david
