Forum: Ruby-Gnome 2 VisualRuby - How to clear the rows in listview

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3bd5c8d34ad61a79f251e2ec2a8b38f1?d=identicon&s=25 John S. (Guest)
on 2014-04-17 05:31
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.

C37e335e7250698ebcc8a092a4baded3?d=identicon&s=25 Eric C. (Guest)
on 2014-04-17 21:40
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,
3bd5c8d34ad61a79f251e2ec2a8b38f1?d=identicon&s=25 John S. (Guest)
on 2014-04-17 22:43

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].....

3bd5c8d34ad61a79f251e2ec2a8b38f1?d=identicon&s=25 John S. (Guest)
on 2014-04-17 22:49

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!!!
C37e335e7250698ebcc8a092a4baded3?d=identicon&s=25 Eric C. (Guest)
on 2014-04-18 00:04
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.

3bd5c8d34ad61a79f251e2ec2a8b38f1?d=identicon&s=25 John S. (Guest)
on 2014-04-19 19:00
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,
C37e335e7250698ebcc8a092a4baded3?d=identicon&s=25 Eric C. (Guest)
on 2014-04-22 00:48
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.

C37e335e7250698ebcc8a092a4baded3?d=identicon&s=25 Eric C. (Guest)
on 2014-04-22 01:01
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.

Fdb50c535854cea7cca0d756ccf89308?d=identicon&s=25 Daisy L. (Guest)
on 2014-12-15 04:29
Thank you for your help!

This is a very good solution.

This topic is locked and can not be replied to.