0

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

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

如何将“列”添加到可以使用 gtk_tree_store_set 设置但不会呈现或呈现为列的 gtk_tree_view?当前正在使用如下代码添加列:

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);
4

1 回答 1

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;
}
于 2013-03-06T07:09:21.517 回答