要仅存储数据而不在GtkTreeView中显示它们,您只需使用模型(GtkTreeStore
、GtkListStore
等),也就是说,不要在模型中为该特定属性添加列。
这是gtk3-demo的略微修改版本,用于显示如何在GtkListStore中存储 2 个“列” ,但在GtkTreeView(严重性列)中仅显示一个。注意功能create_model
和add_columns
。
#include <gtk/gtk.h>
enum
{
COLUMN_NUMBER,
COLUMN_SEVERITY,
NUM_COLUMNS
};
static GtkTreeModel *
create_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (NUM_COLUMNS,
G_TYPE_UINT,
G_TYPE_STRING);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_NUMBER, 1,
COLUMN_SEVERITY, "Normal", -1);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_NUMBER, 2,
COLUMN_SEVERITY, "Critical", -1);
return GTK_TREE_MODEL (store);
}
static void
add_columns (GtkTreeView *treeview)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Severity", renderer,
"text", COLUMN_SEVERITY,
NULL);
gtk_tree_view_append_column (treeview, column);
}
int
main (int argc, char **argv)
{
GtkWidget *window = NULL;
GtkTreeModel *model = NULL;
GtkWidget *treeview;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 280, 250);
g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
model = create_model ();
treeview = gtk_tree_view_new_with_model (model);
g_object_unref (model);
add_columns (GTK_TREE_VIEW (treeview));
gtk_container_add (GTK_CONTAINER (window), treeview);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}