Re: Bind GtkTreeView row to some object

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

 



On Wed, Nov 26, 2014 at 10:59:01PM +0100, John Tall wrote:
> I have a GtkTreeView that is used to display some data. Let's say that
> I have a class that defines a person, and that I have a number of
> objects representing different persons. I want to display the name of
> each person in the tree view so I add a row for each person and set
> the value of the first column to the name of the person.
> 
> GtkTreeIter iter;
> GtkListStore *store = ...;
> 
> gtk_list_store_append (liststore, &iter);
> gtk_list_store_set (liststore, &iter, 0, x_person_get_name (person), -1);
> 
> This works fine. But let's say that I want to select a row in the tree
> view and show a dialog with more information on the person. So I
> connect to the row-activated signal and implement the callback. But
> this is where I need a way to get that person object back. I can't
> look up the object based on the name of the person, because there can
> be multiple persons with the same name.
> 
> Is it possible to bind a row in the tree view, or I guess technically
> the list store, to my object so that I can figure out which object the
> row represents?

Don't store the name in the list store; instead, put the object there.
Use a G_TYPE_POINTER or G_TYPE_OBJECT column (note you need to unref
the object after each time you fetch it with gtk_tree_model_get() in the
latter case).  The name, or any other property, can be rendered using a
cell data function set with

    gtk_tree_view_column_set_cell_data_func(...);

The cell data functions fetches the object with gtk_tree_model_get() and
sets some properties (e.g. "text") of the provided GtkCellRenderer
accordingly.

For selection, there is then no problem as you again fetch the object
directly with gtk_tree_model_get().

This is how most my treeviews work anyway.  The direct binding of values
in the model to the things displayed in the view is IMO useful only in
the simplest cases.

Regards,

Yeti

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list




[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux