GtkTreeView: displaying structures and sorting
- From: martine cs washington edu
- To: gtk-app-devel-list gnome org
- Subject: GtkTreeView: displaying structures and sorting
- Date: Wed, 10 Apr 2002 23:37:14 -0700
I have what seems to be a pretty common situation:
I have a struct with a few fields. I get an array of these structs and
I want to display them in a list-like form.
It seems there are a few ways of doing this:
- create a list store with the appropriate fields:
gtk_list_store_new(n, ...);
where n is the number of fields I'm trying to display.
Then, I iterate through the list of structs and call
gtk_list_store_append() and gtk_list_store_set() to fill in these
fields. This is undesireable because I am copying the data from my
structures to the list widget, and changing the data in the
structures will not affect the data displayed in the list.
- or, I can make my structure into a boxed type, as was done in the
gtk stock demonstration. However, this again appears to copy the structure
to the list whenever I append.
- or, I can create a list store that stores a pointer to the structures:
gtk_list_store_new(1, G_TYPE_POINTER)
and write code similar to the boxed type code; for example, here I
supply the data to display the "username" field of the "Friend"
structure.
static void
user_name_data_func(GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
Friend *friend;
gtk_tree_model_get(model, iter,
0, &friend,
-1);
g_object_set(cell,
"text", friend->username,
NULL);
}
this function is attached to the column like so:
gtk_tree_view_column_set_cell_data_func(column, cell_renderer,
user_name_data_func, NULL, NULL);
and when I destroy my list, I free all of the structures:
static gboolean
model_free_friend(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter
*iter, gpointer data) {
Friend *friend;
gtk_tree_model_get(model, iter,
0, &friend,
-1);
friend_free(friend);
return FALSE;
}
.../* destroying */...
gtk_tree_model_foreach([the list store],
model_free_friend, NULL);
This final solution seems ideal; however, this completely loses the
advantage of the first solution: GTK doesn't know how to properly sort
each of my display columns.
I've scoured the documentation and glanced over the mailing list, and
I'm surprised this isn't a FAQ. At least in the context of this
program, there are many different places where I want to display some
sort of list of structures, and I imagine that most other programs would
too.
So how should I be doing it? (Assume that my structure is sorta
heavyweight and I'd like to avoid copying the data needlessly if at all
possible.)
And, finally, while I have your attention: even if I pick the first
option, accepting that the list store will duplicate all of my data and
I'm careful to refresh the data when it changes, how can I sort data
that is displayed by a Pixbuf? (For example, I have a "type" column,
which displays an icon representing the type; sorting on it should at
least group the rows by their type, and I'd like to choose the order
myself.)
Thanks!
--
Evan Martin
martine cs washington edu
http://neugierig.org
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]