2013-03-06 49 views
0

我正在修改具有GUI的C/Gtk +程序,該列表界面顯示由gtk_tree_store和gtk_tree_view備份的一系列行和列。gtk_tree_store如何添加未呈現爲列的元素

基於與此程序的交互,我想將模型/商店中的其他值與現有行/條目一起存儲,但我不希望這些值顯示在列中。沒有其他現有的數據結構包含我可以使用的這些條目。

如何添加一個「列」到gtk_tree_view,我可以使用gtk_tree_store_set進行設置,但不會呈現或呈現爲列?目前正在添加的代碼看起來像這樣的列:

renderer = gtk_cell_renderer_text_new(); 

g_object_set(
    renderer, 
    "foreground", 
    "white", 
    NULL); 


column = gtk_tree_view_column_new_with_attributes (
    "ColX", 
    renderer, 
    "text", 
    COLUMN_ID_X, 
    "foreground", 
    SOME_COLOR, 
    "foreground-set", 
    COLOR_SET_COLUMN, 
    NULL); 

gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); 

回答

0

要只存儲數據沒有顯示出他們在GtkTreeView你只需要使用該模型(GtkTreeStoreGtkListStore等),也就是說,不在模型中爲該特定屬性添加一列。

這裏gtk3-demo略加修改以顯示如何存儲在GtkListStore 2「列」,但只示出了一個在GtkTreeView(所述嚴重性列)。請注意功能create_modeladd_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; 
}