Re: Is it possible to store pointers in Gtk::Liststore?



On Mon, 22 Jun 2009 21:20:01 +0200
Germán Diago <germandiago gmail com> wrote:

> Hello. I would like to know if there is any workaround to store
> pointers in a ListStore? I tried directly, but I don't know how to
> do it?
> 
Yes, it's simple. You just declare the pointer as one of the columns in
your model. Here's my model columns class, containing an arbitrary
pointer called "dataitem" :

class DLF_ModelColumns : public Gtk::TreeModel::ColumnRecord
{
 public:
   Gtk::TreeModelColumn<Glib::ustring>              varname;
   Gtk::TreeModelColumn<Glib::ustring>              value;
   Gtk::TreeModelColumn<Glib::ustring>              units;
   Gtk::TreeModelColumn<Glib::ustring>              description;
   Gtk::TreeModelColumn<void*>            dataitem;

   DLF_ModelColumns() { 
       add(varname); add(value); add(units); add(description);
       add(dataitem);
   }
};

Then create your ListStore the normal way:

    DLF_Columns = new DLF_ModelColumns;
    Items = Gtk::ListStore::create ( DLF_Columns );

And when you add a row, you set the pointer:

    Gtk::TreeModel::iterator iter = Items->append();
    Gtk::TreeModel::Row row = *iter;

    nsi = new DLF_ScreenItem ( newItem, this, iter );
    row[DLF_Columns.dataitem] = (void*)nsi;
    row[DLF_Columns.varname] = newItem->GetName();
    row[DLF_Columns.value] = "--";
    row[DLF_Columns.units] = "--";
    row[DLF_Columns.description] = "--";
    // etc...

I've used a void* above because I was having trouble with pointers to
complex objects, but I think that was due to having a static
DLF_ModelColumns instance instead of instantiating it when needed. It
should be OK to include a pointer to anything you want.

The issue, of course, is how to handle displaying that pointer. In my
case, I don't want to (it's a pointer to an object that provides values
with which to update other columns), so I simply don't add that column
to the TreeView (don't call append_column for it). If it is meaningful
for display, it probably needs a custom CellRenderer, but that's beyond
my experience.

Hope that helps

Rob


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