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

View: New views
5 Messages — Rating Filter:   Alert me  

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

by Germán Diago :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

Another solution would be to specialize a Gtk::TreeViewColumn<>
but I don't know if this can be done.
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

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

by Robert Pearce-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 22 Jun 2009 21:20:01 +0200
Germán Diago <germandiago@...> 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
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

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

by Germán Diago :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.

I don't know why, but I think my problem was similar. Now storing a pointer
works for me. Then I just had to implement my own cell_data_func and done.
Thank you very much. It helped.
 
 
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list


_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

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

by Daniel Elstner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am Montag, den 22.06.2009, 23:32 +0200 schrieb Germán Diago:
>
>         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.

It can be static, as long as a it isn't instantiated before Gtk::Main.
What I usually do is to have a function which simply returns a reference
to a static variable defined inside the function body.

> I don't know why, but I think my problem was similar. Now storing a
> pointer
> works for me. Then I just had to implement my own cell_data_func and
> done.
> Thank you very much. It helped.

Yes, it should just work.  There is a partial specialization for pointer
types which takes care of that.

On a side note, you might want to use a smart pointer instead, or the
memory management will get hairy.  But I don't know your circumstances,
so maybe this doesn't apply.

--Daniel


_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

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

by Michael Hasselmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am Dienstag, den 23.06.2009, 16:30 +0200 schrieb Daniel Elstner:

> Am Montag, den 22.06.2009, 23:32 +0200 schrieb Germán Diago:
> >
> >         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.
>
> It can be static, as long as a it isn't instantiated before Gtk::Main.
> What I usually do is to have a function which simply returns a reference
> to a static variable defined inside the function body.

Also see Commandme^WItem 4 of the superb (and very well-written)
"Effective C++ Third Edition" of Scott Meyers.

> On a side note, you might want to use a smart pointer instead, or the
> memory management will get hairy.  But I don't know your circumstances,
> so maybe this doesn't apply.

Item 13, same book. Really, this book is a must-read (says a C++ newbie,
heh!)

regards,
Michael

_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list