1
我寫在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);
,並查看它在一個TreeView(在一個單獨的功能)。樹形視圖是:
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是寫在TreeView本身的數據功能:
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);
}
}
問題是我也想更新文件中的數據。我可以得到一些關於如何做到這一點的幫助?