GtkTreeView questions

  1. How do I associate some data with a row in the tree?

    Remember that the GtkTreeModel columns don’t necessarily have to be displayed. So you can put non-user-visible data in your model just like any other data, and retrieve it with gtk_tree_model_get(). See the tree widget overview.

  2. How do I put an image and some text in the same column?

    You can pack more than one GtkCellRenderer into a single GtkTreeViewColumn using gtk_tree_view_column_pack_start() or gtk_tree_view_column_pack_end(). So pack both a GtkCellRendererPixbuf and a GtkCellRendererText into the column.

  3. I can set data easily on my GtkTreeStore or GtkListStore models using gtk_list_store_set() and gtk_tree_store_set(), but can’t read it back?

    Both the GtkTreeStore and the GtkListStore implement the GtkTreeModel interface. As a consequence, you can use any function this interface implements. The easiest way to read a set of data back is to use gtk_tree_model_get().

  4. How do I change the way that numbers are formatted by GtkTreeView?

    Use gtk_tree_view_insert_column_with_data_func() or gtk_tree_view_column_set_cell_data_func() and do the conversion from number to string yourself (with, say, g_strdup_printf()).

    The following example demonstrates this:

    enum
    {
      DOUBLE_COLUMN,
      N_COLUMNS
    };
    
    GtkListStore *mycolumns;
    
    GtkTreeView *treeview;
    
    void
    my_cell_double_to_text (GtkTreeViewColumn *tree_column,
                            GtkCellRenderer   *cell,
                            GtkTreeModel      *tree_model,
                            GtkTreeIter       *iter,
                            gpointer           data)
    {
      GtkCellRendererText *cell_text = (GtkCellRendererText *)cell;
      double d;
      char *text;
    
      /* Get the double value from the model. */
      gtk_tree_model_get (tree_model, iter, (int)data, &d, -1);
      /* Now we can format the value ourselves. */
      text = g_strdup_printf ("%.2f", d);
      g_object_set (cell, "text", text, NULL);
      g_free (text);
    }
    
    void
    set_up_new_columns (GtkTreeView *myview)
    {
      GtkCellRendererText *renderer;
      GtkTreeViewColumn *column;
      GtkListStore *mycolumns;
    
      /* Create the data model and associate it with the given TreeView */
      mycolumns = gtk_list_store_new (N_COLUMNS, G_TYPE_DOUBLE);
      gtk_tree_view_set_model (myview, GTK_TREE_MODEL (mycolumns));
    
      /* Create a GtkCellRendererText */
      renderer = gtk_cell_renderer_text_new();
    
      /* Create a new column that has a title ("Example column"),
       * uses the above created renderer that will render the double
       * value into text from the associated model's rows.
       */
      column = gtk_tree_view_column_new();
      gtk_tree_view_column_set_title  (column, "Example column");
      renderer = gtk_cell_renderer_text_new();
      gtk_tree_view_column_pack_start (column, renderer, TRUE);
    
      /* Append the new column after the GtkTreeView's previous columns. */
      gtk_tree_view_append_column (GTK_TREE_VIEW (myview), column);
      /* Since we created the column by hand, we can set it up for our
       * needs, e.g. set its minimum and maximum width, etc.
       */
      /* Set up a custom function that will be called when the column content
       * is rendered. We use the func_data pointer as an index into our
       * model. This is convenient when using multi column lists.
       */
      gtk_tree_view_column_set_cell_data_func (column, renderer,
                                               my_cell_double_to_text,
                                               (gpointer)DOUBLE_COLUMN, NULL);
    }
    
  5. How do I hide the expander arrows in my tree view?

    Set the expander-column property of the tree view to a hidden column. See gtk_tree_view_set_expander_column() and gtk_tree_view_column_set_visible().