GtkTreeViewのGTK_SELECTION_MULTIPLEモード(その2)(想定通り動かないもの)
GtkTreeView の GTK_SELECTION_MULTIPLE モードで、 gtk_tree_selection_selected_foreach() から呼び出される関数ないで、 gtk_list_store_remove() を使うと、
(m_treeview:2693): Gtk-WARNING **: The model has been modified from within gtk_tree_selection_selected_foreach. This function is for observing the selections of the tree only. If you are trying to get all selected items from the tree, try using gtk_tree_selection_get_selected_rows instead.
のようなエラーが出る。これは、一つずつ選択された項目を処理しながら、リストを変更しているためらしい。
これを防ぐためには、 gtk_tree_selection_get_selected_rows() を使うと良いらしい。
(GtkTreeView *treeview) GtkTreeSelection *selection; GList *list; selection = gtk_tree_view_get_selection (treeview); /* list には、GtkTreePath 型のリストは入ってくる */ list = gtk_tree_selection_get_selected_rows (selection, NULL); /* 必ずクリーンアップ*/ g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL); g_list_free (list);
実際には list を使って g_list_foreach() 関数内で削除する項目を取得して、削除していくのが良いと思います。 gtk_tree_path_get_indices() で、GtkTreeView の順番が取得できます。残念ながら、 GtkTreePath の扱いって devhelp にはあまり記載されていないんですよね。ということで、想定通り動かないけど、代替手段はありそうだってことで終了します。