Re: [gtkmm] suffering from slow treeview



gtkmm-list wrote:

Thanks for the suggestion, it does speed thing up, but afais only when
you use a single color. I need alternating colors ( green,red,yellow) to
indicate the states of persons in list. And for that i need the line in Add() .

However, i found another solution for my problem today. i now use some
images from the Gtk::Stock collection icw render_icon() to indicate a
state. It's as clear as backgroundcolors and performance is MUCH better.

Still, i'm curious if there is a solution to this slow background
drawing. Any more ideas?

Yes. Try the attached variant. You'll notice it manages two colours quite nicely, and I've not noticed any performance problems with it. The background colour property is read from the color column in each row of the TreeStore individually, just as the text property is, so you can have as many colours as you like. If you don't understand why, I would recommend reading the TreeView chapters of the gtkmm tutorial/book available on http://www.gtkmm.org, and the various examples in the distribution which use TreeViews (the stock browser, the TreeView demos etc).

I may have changed a few std::strings to Glib::ustrings here and there, but that's me being pedantic and shouldn't have any real effect on the functioning of the program. Try it and see.
#include <gtkmm/treeview.h>
#include <gtkmm/treestore.h>
#include <gtkmm/window.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/main.h>
#include <gtkmm/scrolledwindow.h>

class DemoTree : public Gtk::TreeView
{
  private:
    struct Columns : public Gtk::TreeModelColumnRecord             
    {
      Gtk::TreeModelColumn<Glib::ustring> column1;
      Gtk::TreeModelColumn<Glib::ustring> color;
      Columns() { add(column1);add(color); }
    };
    Columns columns;

    Gtk::CellRendererText* cell_renderer_text;
    Gtk::TreeViewColumn* color_column;
    Gtk::TreeRow row;


  public:
    Glib::RefPtr<Gtk::TreeStore> treestore;

    DemoTree()
    {
      treestore = Gtk::TreeStore::create( columns );
      set_model( treestore );

      append_column("DemoColumn", columns.column1);

      color_column = get_column(0);
      Gtk::CellRenderer* cell_renderer = color_column->get_first_cell_renderer();
      cell_renderer_text = dynamic_cast<Gtk::CellRendererText*>(cell_renderer);
      color_column->add_attribute(cell_renderer_text->property_background(), columns.color);
    }

    void AddRow( const Glib::ustring &name, const Glib::ustring &colour )
    {		
      row = *(treestore->append());
      row[columns.color] = colour;

      row[columns.column1] = name;
    }


};

class DemoWindow : public Gtk::Window
{
  DemoTree dt;
  Gtk::ScrolledWindow scroll_window;
  Gtk::VBox vbox_main;
  Gtk::Button btn_Refresh;

  public:	
  DemoWindow()
  {
    set_default_size( 400, 600 );

    scroll_window.add( dt );
    scroll_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    vbox_main.pack_start( scroll_window );

    btn_Refresh.set_label("Refresh");
    btn_Refresh.signal_clicked().connect( SigC::slot(*this, &DemoWindow::btn_refresh_click) );
    vbox_main.pack_start( btn_Refresh, Gtk::PACK_SHRINK );


    btn_refresh_click();//fill 'er up :P

    add(vbox_main);
    show_all_children();
  }

  void btn_refresh_click()
  {
    dt.treestore->clear();
    for(int i = 0; i < 200; i++) dt.AddRow("TESTING", "blue");
    for(int i = 0; i < 50; i++) dt.AddRow("TESTING", "red");
  }
};


int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  DemoWindow dw;
  Gtk::Main::run(dw);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]