SegFault on a GtkTreeSelection changed
- From: Nicolas Soubeiran <nicolas soubeiran gmail com>
- To: gtk-app-devel-list gnome org
- Subject: SegFault on a GtkTreeSelection changed
- Date: Wed, 24 Mar 2010 10:22:40 +0100
Hello,
I have trouble with a sorted model when deleting some row from the child
model :
the following code SegFault when you select the 2 first lines of the
treeview then click the button (remove the first line of the child model).
What should I do to avoid this
#include <gtk/gtk.h>
#define STRING_COLUMN 0
#define NB_ELEMENT 10
static GtkWidget* _init_window( )
{
GtkWidget* top_level = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(top_level),
"delete-event", G_CALLBACK(gtk_main_quit), NULL);
return top_level;
}
static GtkTreeModel* _create_sorted_model(GtkTreeModel* model)
{
GtkTreeModel* sorted_model = gtk_tree_model_sort_new_with_model(model);
return sorted_model;
}
void _selection_print(GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter, gpointer data)
{
gchar* str_data;
gtk_tree_model_get(model, iter, STRING_COLUMN, &str_data, -1); /* SEG V */
g_print(str_data);
g_print("\n");
g_free(str_data);
}
static void _print_selected_cbk (GtkTreeSelection *treeselection)
{
if(gtk_tree_selection_count_selected_rows(treeselection) == 1)
{
gtk_tree_selection_selected_foreach(treeselection, _selection_print,
NULL);
}
}
static GtkTreeModel* _create_treemodel()
{
GtkListStore* model = gtk_list_store_new(1, G_TYPE_STRING);
gint i;
for(i = 0; i< NB_ELEMENT; ++i)
{
GtkTreeIter iter;
const gchar* element = "element id";
gtk_list_store_append(model, &iter);
gtk_list_store_set(model, &iter, STRING_COLUMN, element, -1);
}
return GTK_TREE_MODEL(model);
}
static GtkWidget* _create_treeview(GtkTreeModel* model)
{
GtkWidget* tree_view;
GtkTreeSelection* selection;
GtkCellRenderer* cell_renderer = gtk_cell_renderer_text_new();
GtkTreeModel *sorted_model = _create_sorted_model(model);
tree_view = gtk_tree_view_new_with_model(sorted_model);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view), -1,
"name",
cell_renderer, "text", STRING_COLUMN, NULL);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
g_signal_connect(G_OBJECT(selection),
"changed", G_CALLBACK(_print_selected_cbk), NULL);
return tree_view;
}
static void _delete_first_row(GtkWidget* button, GtkListStore* list)
{
GtkTreeIter iter;
if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(list), &iter))
{
gtk_list_store_remove(list, &iter);
}
}
gint main(gint argc, gchar** argv)
{
GtkWidget* top_level;
GtkWidget* tree_view;
GtkTreeModel* model;
GtkWidget* vbox;
GtkWidget* button;
GtkWidget* scrolled_window;
gtk_disable_setlocale();
gtk_init(&argc,&argv);
top_level = _init_window();
model = _create_treemodel();
tree_view = _create_treeview(model);
button = gtk_button_new_with_label("Delete first row");
vbox = gtk_vbox_new(5, FALSE);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
/* note that model is not the sorted model but its child*/
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(_delete_first_row), model);
gtk_container_add(GTK_CONTAINER(top_level), vbox);
gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(scrolled_window), tree_view);
gtk_widget_show_all(top_level);
gtk_main();
return 0;
};
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]