GTKCellRendererToggle 在 GTKTreeView 中
GTKCellRendererToggle in a GTKTreeView
for(int i=0; i<2; i++) {
types[i] = G_TYPE_STRING;
}
types[2] = G_TYPE_BOOLEAN;
tree_store = gtk_list_store_newv(3, types);
tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), tree_view);
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("Level",renderer,"text",0,NULL);
gtk_tree_view_column_set_sort_column_id (column, 0);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("URL",renderer,"text",1,NULL);
gtk_tree_view_column_set_sort_column_id (column, 1);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
renderer = gtk_cell_renderer_toggle_new();
column = gtk_tree_view_column_new_with_attributes ("Image",renderer,"active",2,NULL);
gtk_tree_view_column_set_sort_column_id (column, 2);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
for(int i=0; i<mapTiles.size(); i++) {
GtkTreeIter iter;
gtk_list_store_append(tree_store, &iter);
gtk_list_store_set(tree_store, &iter, 0, "A", -1);
gtk_list_store_set(tree_store, &iter, 1, "B", -1);
gtk_list_store_set(tree_store, &iter, 2, FALSE, -1);
}
这是为什么?
我正在尝试创建一个树视图,其中包含两列,末尾带有字符串和一个复选框。现在,复选框出现了,但我无法切换它。
单元格渲染器(切换)仅反映模型上 'binded' 的值。您必须使用回调来处理 toggled 信号,该回调更新单元格渲染器将从中读取其状态的模型。检查 CellRendererToggle 'toggled' signal reference:
void user_function (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data)
It is the responsibility of the application to update the model with
the correct value to store at path . Often this is simply the opposite
of the value currently stored at path .
编辑
使用最后一个渲染器指针,设置切换回调并在回调上更改模型以更新值:
[your code]
...
renderer = gtk_cell_renderer_toggle_new();
...
g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(on_toggle_renderer_toggled), tree_view);
...
void on_toggle_renderer_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data) {
gboolean val;
GtkTreeIter iter;
GtkTreeModel *model;
model = gtk_tree_view_get_model (GTK_TREE_VIEW(user_data));
if (gtk_tree_model_get_iter (model, &iter, path) == false) return;
gtk_tree_model_get(model, &iter, 2, &val, -1);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, !val, -1);
}
PS: 以上代码未经测试,可能有错别字或遗漏。
for(int i=0; i<2; i++) {
types[i] = G_TYPE_STRING;
}
types[2] = G_TYPE_BOOLEAN;
tree_store = gtk_list_store_newv(3, types);
tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), tree_view);
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("Level",renderer,"text",0,NULL);
gtk_tree_view_column_set_sort_column_id (column, 0);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("URL",renderer,"text",1,NULL);
gtk_tree_view_column_set_sort_column_id (column, 1);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
renderer = gtk_cell_renderer_toggle_new();
column = gtk_tree_view_column_new_with_attributes ("Image",renderer,"active",2,NULL);
gtk_tree_view_column_set_sort_column_id (column, 2);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
for(int i=0; i<mapTiles.size(); i++) {
GtkTreeIter iter;
gtk_list_store_append(tree_store, &iter);
gtk_list_store_set(tree_store, &iter, 0, "A", -1);
gtk_list_store_set(tree_store, &iter, 1, "B", -1);
gtk_list_store_set(tree_store, &iter, 2, FALSE, -1);
}
这是为什么? 我正在尝试创建一个树视图,其中包含两列,末尾带有字符串和一个复选框。现在,复选框出现了,但我无法切换它。
单元格渲染器(切换)仅反映模型上 'binded' 的值。您必须使用回调来处理 toggled 信号,该回调更新单元格渲染器将从中读取其状态的模型。检查 CellRendererToggle 'toggled' signal reference:
void user_function (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data)
It is the responsibility of the application to update the model with the correct value to store at path . Often this is simply the opposite of the value currently stored at path .
编辑 使用最后一个渲染器指针,设置切换回调并在回调上更改模型以更新值:
[your code]
...
renderer = gtk_cell_renderer_toggle_new();
...
g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(on_toggle_renderer_toggled), tree_view);
...
void on_toggle_renderer_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data) {
gboolean val;
GtkTreeIter iter;
GtkTreeModel *model;
model = gtk_tree_view_get_model (GTK_TREE_VIEW(user_data));
if (gtk_tree_model_get_iter (model, &iter, path) == false) return;
gtk_tree_model_get(model, &iter, 2, &val, -1);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, !val, -1);
}
PS: 以上代码未经测试,可能有错别字或遗漏。