VisualRuby - How to clear the rows in listview

In my program, I need to display data which periodically changes, that
the number of rows and content in the listview completely change.

I would like to clear the rows in the listview and re-populate the
information for the data source. The columns still remain the same.

Can you give me an example or snippet of code to clear the rows.


I usually write a refresh() method that deletes all the records, then
fills it in again. I’ve had no speed problems because I’ve never done
huge tables.

You can use the clear() method on the model.


Here is the code from visualruby where the “Choose Project” dialog box
appears. The listview is populated with all the visualruby projects
found on your hard disk:

def refresh(show_backup = false)
pattern = “”
backup_path = Dir.pwd.gsub(ENV[“HOME”], File.join(ENV[“HOME”],
backup_ar = File.split(backup_path)
backup_ar << ["", VR_ENV::SETTINGS_FILE]
if show_backup
pattern = File.join(*backup_ar)
pattern = File.join(ENV[“HOME”], “visualruby”, "
Dir.glob(pattern, File::FNM_DOTMATCH).each do |fn|
next if not show_backup and fn =~ / Backup /
next if show_backup and not fn =~ / Backup /
t = File.stat(fn).mtime
row = add_row()
row[:pix] = PIX
row[:file_name] = File.dirname(fn)
row[:modified] = DateTime.parse(t.to_s)
model.set_sort_column_id(id(:modified), Gtk::SORT_DESCENDING)

Hope that helps,


The “self.model.clear” worked, it cleared out all of the lines in
the listview and program was able to repopulate the list with new

What command is used to clear one line in the listview ( ie del 5th

Is it possible to change the background of just one field in the
ie - row[:status]…


Yes, you can change the background color. See the listview example
program. Go to Open project. Look in the examples folder for the
listview. It has a button to change the background to yellow.

If I remember correctly, its difficult to do individual cells, but there
is a way.



FYI: A listview with 4000 entries and 6 fields was able to clear
with the “self.model.clear” command and repopulated
with just a quick blink of the display (no pause). Impressive!!!

Hi Eric,

Looked at “listview” example, it changes the background color
of the whole column.

I would like to change only the color of a specific item in the row,
something like row[:name].setbackgroundcolor=“red”

Thank you for your help,

I found this:

Seems hokey. I don’t know the answer now. I think I remember looking
at this issue, but I can’t remember the answer.


Ok I found it. When you define an object to be a column in a listview,
you create a method called visual_attributes(). This method returns a
hash with the attributes in it. e.g. hash[:foreground] = “white”.

Look at this example from the active_record2 project found in the
examples folder:

class Employee < ActiveRecord::Base
has_many :paychecks
belongs_to :employer

include GladeGUI

def visual_attributes
bal = balance().to_f
hash = { :text => name }
hash[:foreground] = (bal < 0) ? “white” : “black”
hash[:background] = (bal < 0) ? “red” : “white”
return hash

def balance()
Paycheck.sum(:amount, :conditions => “employee_id = #

Paycheck.find(“employee_id = #{id}”).calculate(:sum,



This project isn’t working right now. I beleive its due to activerecord
constantle changing their syntax on me. Its not summing the balance
correctly. Perhaps someone can debug the balance() method for me. When
the balance() method returns a negative value, the individual cell will
appear red.

I think you’ll agree that this is a very good solution.


Thank you for your help!

This is a very good solution.

Hi, have you checked this out

The following ruby forum resolves the technical issues through conversation.Thanks for the outstanding support and help.I have two queries that how I can build applications with
Ruby and How could I use the new sign in page for office 365 login?

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

Sponsor our Newsletter | Privacy Policy | Terms of Service