gtk_tree_store如何添加未呈现为列的元素

时间:2013-03-06 05:47:10

标签: c gtk

我正在修改一个C / Gtk +程序,该程序具有一个带有列表界面的GUI,该界面显示由gtk_tree_store和gtk_tree_view备份的一系列行和列。

基于与此程序的交互,我希望在模型/存储中存储其他值以及现有行/条目,但是我不希望这些值显示在列中。没有其他现有的数据结构包含我可以使用的这些条目。

如何向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);

1 个答案:

答案 0 :(得分: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;
}