在文件中的 gtk 树视图单元格中写入更改



我正在用 gtk 编写代码,打开一个文件,将其解析为

/*Open and scan the file*/
fd = open (filename, O_RDWR);
scanner = g_scanner_new (NULL);
g_scanner_input_file (scanner, fd);
....
....
g_scanner_destroy (scanner);
gtk_label_set_text(GTK_LABEL(flabel), filename);
close (fd);  

并在树视图中查看它(在单独的函数中)。树视图如下所示:

store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING,
                                                G_TYPE_STRING, 
                                                G_TYPE_STRING, 
                                                G_TYPE_STRING);
  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
/* #1: KEY COLUMN */  
  cell = gtk_cell_renderer_text_new ();
  g_object_set (cell,
                "editable", TRUE,
                NULL);
  g_signal_connect (cell, 
      "edited",G_CALLBACK(cell_edited), 
      tree);
  g_object_set_data (G_OBJECT (cell), 
      "column", GINT_TO_POINTER (COL_BIB_KEY));
  GtkTreeViewColumn *col_key,*col_year,*col_type,*col_auth,*col_pub;
  col_key=gtk_tree_view_column_new_with_attributes (
      "Key", cell,
      "text", COL_BIB_KEY,
      NULL);
  gtk_tree_view_column_set_sort_column_id( col_key, ID_KEY);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree), col_key);
  gtk_tree_view_column_set_max_width  (col_key,100);

其中cell_edited是在树视图本身中写入数据的函数:

void cell_edited(GtkCellRendererText *renderer,
    gchar *path,
    gchar *new_text,
    GtkTreeView *treeview)
{
  guint column;
  GtkTreeIter iter;
  GtkTreeModel *model;
  gpointer columnptr = g_object_get_data(G_OBJECT(renderer), "column");
  column = GPOINTER_TO_UINT(columnptr);
  if (g_ascii_strcasecmp (new_text, "") != 0)
  {
    model = gtk_tree_view_get_model (treeview);
    if (gtk_tree_model_get_iter_from_string (model, &iter, path))
      gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);
  }
}

问题是我还想更新文件中的数据。我可以得到一些关于如何做到这一点的帮助吗?

您必须以所需的相同格式构建文件的内容,然后将其写出来。

一种方法是使用 GString 构建内容,然后使用 g_file_set_contents 将其写出。

// Create GString
GString *str = g_string_new ("");
// Iterate over the list store
GtkTreeIter iter;
gboolean valid;
for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
     valid;
     valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)) {
    char *string0, *string1, *string2, *string3, *string4;
    gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
                        0, &string0,
                        1, &string1,
                        2, &string2,
                        3, &string3,
                        4, &string4,
                        -1);
    // I don't know the format of your file, so I'm just going to separate them with a comma
    g_string_append_printf (str, "%s,%s,%s,%s,%sn", string0, string1, string2, string3, string4);
    // gtk_tree_model_get copies strings, so free them
    g_free (string0);
    g_free (string1);
    g_free (string2);
    g_free (string3);
    g_free (string4);
}
// Set the file contents atomically, add error checking
g_file_set_contents (filename, str->str, -1, NULL);
// Free the GString
g_string_free (str, TRUE);

相关内容

  • 没有找到相关文章