c - Creating a simple gtk list -
i create simple table in gtk following content:
| test name | 0 | 10 |
i want create this, gtk doesn't make easy. have made gtkliststore contains information need, apparently need create gtktreeviewcolumn well. need pass values gtk_tree_view_column_new_with_attributes, problem don't know attribute names , can't find them anywhere. attribute found "text", pass unsigned integers well. tell me these attributes are, , how create 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 posted code following result:
| name ? | | test name |
you must understand model , view separated concepts.
gtkliststore
model , contains data shown. can have bunch of other data not needed view, e.g. can result of more general sql query.
gtktreeview
view , must instruct on how present data on screen. packing different columns inside rectangular region. following example gives idea of can accomplished:
#include <gtk/gtk.h> enum { file_name, file_offset, file_size, file_description, /* not used view, maybe used elsewhere */ color, /* show how model can affect 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; }
Comments
Post a Comment