Creating a simple gtk list

c, gtk, gtk3, linux

Solution

You must understand model and view are separated concepts.

`GtkListStore` is the model and contains the data to be shown. It can also have a bunch of other data not needed by the view, e.g. it can be the result of a more general SQL query.

`GtkTreeView` is the view and you must instruct it on how to present the data on the screen. It does it by packing different columns inside a rectangular region. The following example gives you an idea of what can be accomplished:

#include <gtk/gtk.h>

enum {
    FILE_NAME,
    FILE_OFFSET,
    FILE_SIZE,
    FILE_DESCRIPTION, /* Not used by the view, maybe used elsewhere */
    COLOR,            /* Just to show how the model can affect the view */
    N_COLUMNS
};

gint main(gint argc, gchar **argv)
{
    GtkListStore*      model;
    GtkWidget*         view;
    GtkTreeViewColumn* column;
    GtkWidget*         window;

    gtk_init(&argc, &argv);

    /* MODEL */
    model = gtk_list_store_new(N_COLUMNS,
                               G_TYPE_STRING,   /* FILE_NAME */
                               G_TYPE_UINT,     /* FILE_OFFSET */
                               G_TYPE_UINT,     /* FILE_SIZE */
                               G_TYPE_STRING,   /* FILE_DESCRIPTION */
                               G_TYPE_STRING    /* COLOR */
                              );
    gtk_list_store_insert_with_values(model, NULL, -1,
                                      FILE_NAME, "test name",
                                      FILE_OFFSET, 0,
                                      FILE_SIZE, 10,
                                      -1);
    gtk_list_store_insert_with_values(model, NULL, -1,
                                      FILE_NAME, "Dummy",
                                      FILE_OFFSET, 123,
                                      COLOR, "black",
                                      -1);
    gtk_list_store_insert_with_values(model, NULL, -1,
                                      COLOR, "blue",
                                      -1);

    /* VIEW */
    view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
    g_object_unref(model);

    column = gtk_tree_view_column_new_with_attributes("Name",
                                                      gtk_cell_renderer_text_new(),
                                                      "text", FILE_NAME,
                                                      "background", COLOR,
                                                      NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);

    column = gtk_tree_view_column_new_with_attributes("Offset",
                                                      gtk_cell_renderer_spin_new(),
                                                      "text", FILE_OFFSET,
                                                      NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);

    column = gtk_tree_view_column_new_with_attributes("Size",
                                                      gtk_cell_renderer_text_new(),
                                                      "text", FILE_SIZE,
                                                      NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);

    /* MAIN */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_add(GTK_CONTAINER(window), view);
    gtk_widget_show_all(window);

    gtk_main ();

    return 0;
}

Problem

I would like to create a simple table in gtk with the following content: ``` | test name | 0 | 10 | ``` I simply want to create this, but gtk doesn't make this easy. I have made a GtkListStore which contains all the information I need, but apparently I need to create a GtkTreeViewColumn as well. I need to pass the values to gtk_tree_view_column_new_with_attributes, but the problem is that I don't know any attribute names and I can't find them anywhere. The only attribute I found was "text", but I pass unsigned integers as well. Could someone tell me what these attributes are, and how to create a simple table? (I read https://developer.gnome.org/gtk3/stable/TreeWidget.html btw) ``` enum { FILE_NAME = 0, FILE_OFFSET, FILE_SIZE }; GtkWidget* tree; GtkListStore* store; GtkTreeIter iter; GtkCellRenderer* renderer; GtkTreeViewColumn* column; store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT); gtk_list_store_append (store, &iter); gtk_list_store_set (store, &iter, FILE_NAME, "test name", FILE_OFFSET, 0, FILE_SIZE, 10, -1); tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); g_object_unref (G_OBJECT (store)); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes ("Name", renderer, "text", FILE_NAME, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); ``` With the posted code I get the following result: ``` | name ? | | test name | ```

Original source