我想在树视图中放置一个复选框。当我单击复选框时,它应该检查。但是 Checkbox 一直没有被选中。我用 C 和 GTK3 编写了一个示例代码:
#include <gtk/gtk.h>
GtkTreeModel* create_model ()
{
GtkTreeIter gti;
GtkListStore *gls = gtk_list_store_new ( 1, G_TYPE_BOOLEAN );
gtk_list_store_append ( gls, >i );
gtk_list_store_set ( gls, >i, 0, FALSE, -1 );
return GTK_TREE_MODEL ( gls );
}
void check ( GtkCellRendererToggle *cell )
{
int active = gtk_cell_renderer_toggle_get_active ( cell );
g_print ( "%d\n", active );
if ( active )
{
//fail
gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE ( cell ), FALSE );
}
else
{
//fail
gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE ( cell ), TRUE) ;
}
return;
}
int main ( int argc, char *argv[] )
{
gtk_init ( &argc, &argv );
//window
GtkWidget *window_main = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
//tree view
GtkWidget *treeview = gtk_tree_view_new_with_model ( create_model () );
//cell_renderer_toggle
GtkCellRenderer *cell = gtk_cell_renderer_toggle_new ();
gtk_tree_view_append_column ( GTK_TREE_VIEW ( treeview ), gtk_tree_view_column_new_with_attributes ( "test", cell, "active", 0, NULL ) );
g_signal_connect ( cell, "toggled", G_CALLBACK ( check ), NULL );
gtk_container_add ( GTK_CONTAINER ( window_main ), treeview );
gtk_widget_show_all ( window_main );
gtk_main();
return 0;
}
函数 gtk_cell_renderer_toggle_set_active 调用失败并且没有设置单元格激活。请帮我修复我的代码。