Re: How do you get the data from a GList?



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 17, 2006 at 09:44:01AM -0600, Steven Boyls wrote:
I have created a GList with the following code :
   GList                     *my_list;
   GtkTreeSelection  *selection;
   GtkTreeModel      *model;

   selection = gtk_tree_view_get_selection(list_file_treeview);
   model = gtk_tree_view_get_model(list_file_treeview);
   my_list = gtk_tree_selection_get_selected_rows(selection, &model);

I'd do (after having done the above):

     g_list *node);
     for(node=my_list; node; node=node->next) {
       GtkTreeIter iter;
       if(gtk_tree_model_get_iter(model, &iter, node->data)) {
         /* now iter is a valid row iterator */
         GValue val;
         gint col;
         for(col=0; col<gtk_tree_model_get_n_columns; col++) {
           /* assume column 0 is a gchar* and column 1 is a gint.
              See gtk_tree_model_get_value() and _type() for when you
              don't know how the model is built */
           gchar *name; /* col 0 */
           gint age;    /* col 1 */
           gtk_tree_model_get(model, &iter,
                              0, &name,
                              1, &age,
                              -1 /* end */);
           g_message("%s is %d years old", name, age);
           g_free(name);
         }
       }
       gtk_tree_path_free(node->data); /* if you don't need it anymore */
     }
     g_list_free(my_list);

See
<http://developer.gnome.org/doc/API/2.0/gtk/GtkTreeModel.html#gtk-tree-model-get-iter>

for an explanation of how all this works. There is a slightly simpler
way if you just want to iterate throught the selection without modifying
the model:

    selection = gtk_tree_view_get_selection(list_file_treeview);
    model = gtk_tree_view_get_model(list_file_treeview);
    gtk_tree_selection_selected_foreach(selection, selfun, NULL);

and then:

    void selfun(GtkTreeModel *mo, GtkTreePath *path, GtkTreeIter *it, gpointer data)
    {
           gchar *name; /* col 0 */
           gint age;    /* col 1 */
           gtk_tree_model_get(model, &iter,
                              0, &name,
                              1, &age,
                              -1 /* end */);
           g_message("%s is %d years old", name, age);
           g_free(name);
     }


HTH
- -- tomÃs
       
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFXqJoBcgs9XrR2kYRAku7AJoDsQ01jVZ2eOerYxTepT7D0zb3KwCcDRee
55iuaiqP1RDUvJ2hvAogbeI=
=vFff
-----END PGP SIGNATURE-----




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]