Re: GtkTreeView: displaying structures and sorting
- From: Evan Martin <martine cs washington edu>
- To: Jonathan Blandford <jrb redhat com>
- Cc: Havoc Pennington <hp redhat com>, gtk-app-devel-list gnome org
- Subject: Re: GtkTreeView: displaying structures and sorting
- Date: Fri, 12 Apr 2002 16:28:23 -0700
On Fri, Apr 12, 2002 at 01:07:23PM -0400, Jonathan Blandford wrote:
I think the basic answer is that you want to set the sort function. 
You can apparently have an arbitrary number of sort functions; the
"column IDs" for the sort functions are separate IDs from the model or
view column IDs. Or something, it confused me too. ;-)
You can set up custom id's to sort the model by.
So I have code like this (cut'n'paste from different parts in the source
file):
    fdlg->friendstore = GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_POINTER));
    view = gtk_tree_view_new_with_model(fdlg->friendstore);
    g_object_unref(G_OBJECT(fdlg->friendstore));
    ...
    column = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(column, "User");
    cell_renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(column, cell_renderer, TRUE);
    gtk_tree_view_column_set_cell_data_func(column, cell_renderer,
        username_data_func, NULL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
    gtk_tree_view_column_set_sort_column_id(column, FRIEND_COL_USERNAME);
    gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(fdlg->friendstore),
        FRIEND_COL_USERNAME,
        username_sort_func, NULL, NULL);
FRIEND_COL_USERNAME is in an enum, and looks like it's 1 in this case.
The username_* functions are as follows:
static void
username_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);
}
static gint
username_sort_func(GtkTreeModel *model,
                   GtkTreeIter  *a,
                   GtkTreeIter  *b,
                   gpointer      data)
{
    Friend *fa, *fb;
    gtk_tree_model_get(model, a,
            0, &fa,
            -1);
    gtk_tree_model_get(model, b,
            0, &fb,
            -1);
    return strcasecmp(fa->username, fb->username);
}
Then I have almost identical code with "fullname" in the place of
"username", for a second column.
When I click on the column header (to sort, y'know), I get:
(logjam:2068): Gtk-CRITICAL **: file gtkliststore.c: line 1884
(gtk_list_store_set_sort_column_id): assertion `header != NULL' failed
After a lot of attempts at diagnosing the problem and getting very
confused, I accidentally discovered that setting the sort_column_id to 0
would make a single column behave correctly-- maybe it's reading the
data from the 0th column of the list store-- but if I set two columns to
the same sort id everything gets confused (clicking on one column sorts
the other, etc.).
I then tried reducing the list to one column; if I make the list contain
only one column and I set the column to an id other than 0 I get this
error.
Is my code above incorrect in some way?  (FWIW, I have GTK 2.0.2.)
-- 
      Evan Martin
martine cs washington edu
  http://neugierig.org
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]