Re: Associate user_data to a row in GtkTreeView



On Thursday 26 August 2004 12:28, Søren Hansen wrote:

I want to associate some data to each row in a GtkTreeView which is not
showed to the user. A common example would be, that I show the rows in a
table from a database, but not the uniqe ID.

  ... snip example retrieving a string from the row ....

However I would much rather know what ID was selected rather than which
data i showed the user. Is it possible to associate some user_data to
the row, and does anyone have an example on how to do that?

just add additional columns to your list store/ tree store that have the type 
that you want, e.g. G_TYPE_POINTER or G_TYPE_UINT or G_TYPE_UINT64 or 
whatever.

None of the data in your list store/tree store will show up anywhere on the 
screen in your tree view unless you explicitely make it show up, e.g. by 
connecting it to a cell renderer or using it from within a cell data 
function). 

If you don't make it show up, it won't show up, and is nothing more than a 
piece of data associated with a row that you can do with whatever you like.

Example:

Change

     liststore = gtk_list_store_new (1, G_TYPE_STRING);

to

     liststore = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT64);

and then

     gtk_list_store_set (store, &iter,
                                  0, "Some String",
                                  -1);

to

     gtk_list_store_set (store, &iter,
                                  0, "Some String",
                                  1,  (guint64) someid,
                                  -1);


so you can later do

    guint64  myid;

    gtk_tree_model_get (GTK_TREE_MODEL(store), &selected_row_iter,
                                     1, &myid, 
                                     -1);

(replace the model column numbers with COL_STRING, COL_ID etc. enums to make 
the code more legible)

Cheers
 -Tim



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