[Glade-users] Destroy and rebuild a treeview widget

Tristan Van Berkom tvb at gnome.org
Mon Jun 5 11:02:58 EDT 2006


Guillaume Ruch wrote:
[...]
> Hi,
> Once again, thanks for your rapid answer :) In fact, like medhi, I wanted to use clist, but it's
> deprecated so...  I cant really understand treeview/liststore, even with the gtk tutorials, I
> think I'm in the right way, but I know I lack 2 or 3 things to make it work... Can you have a look
> on this?
> 

     You must always go to http://www.gtk.org/api/ or to /usr/include/gtk+-2.0
before randomly calling functions with improvised arguments, otherwise
people like me who just want to be helpfull find that they are doing
everybodies' work for them.

Some errors I noticed follow:

> 
> create_treeview()
> 
>   GtkCellRenderer   	*rend;
>   GtkTreeViewColumn 	*col;
>   GtkWidget    		*listview;
>   GtkListStore 		*liststore;
>   gchar        		*sql, *label;
>   MYSQL_RES    		*res_str;
>   MYSQL_FIELD 		*field;
>   MYSQL_ROW   		 db_row;
>   gint			 i, cols;
>   gchar     *row[20] = {"", "", "", "", "",
>                         "", "", "", "", "",
>                         "", "", ""};
> 
> 
> sql = g_strconcat("select * from ", nom_table, 0L);
>   g_print("sql is: %s\n", sql);
>   if (mysql_query (conn, sql) != 0)
>    {
>       g_print("Echec de la requete...\n");
>       return 0L;
>    }
>   res_str = mysql_store_result (conn);
>  g_print("mysql_store_result...OK\n");
> 
> 
> 
> /**********************Création de la liste****************/
>    cols = mysql_num_fields(res_str);
> 
> 
> 
>    liststore= gtk_list_store_new(gint cols);
>

Since you are using a dynamic list store... you must use gtk_list_store_newv():
http://developer.gnome.org/doc/API/2.0/gtk/GtkListStore.html#gtk-list-store-newv
and specify the data types for the fields in your list store (in your case
G_TYPE_STRING I suppose).

> 
>    while ((db_row = mysql_fetch_row (res_str)) != 0L)
>      {
>         for (i = 0; i < cols; i++)
>           {
>               row[i] = db_row[i];
>           }
>         gtk_list_store_append(liststore, row);

gtk_list_store_append takes a pointer to an allocated GtkTreeIter as
its second argument... the GtkTreeIter will be set to describe the newly
appended row:

     GtkTreeIter iter;

     /* append row */
     gtk_list_store_append(liststore, &iter);

     /* Fill row data in the treemodel */
     gtk_list_store_set (store, &iter,
                         column_number, column_data,
                         column_number, column_data,
                         column_number, column_data,
                         -1); // vararg terminator

> 
>      }
> 
> /* Creation de la vue */
> 
> listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));
> 
> 
> for (i = 0; i < cols; i++)
>     {
>        mysql_field_seek(res_str, i);
>        field = mysql_fetch_field(res_str);
>        label = gtk_label_new (field->name);
>        rend = gtk_cell_renderer_text_new();
>        col = gtk_tree_view_column_new_with_attributes(label,

I believe gtk_tree_view_column_new_with_attributes() takes "const gchar *" as
the first arg... not "GtkLabel *".

>        rend,"text", TEXT_COLUMN, NULL);
>        gtk_tree_view_append_column(GTK_TREE_VIEW(listview), col);
>     }
> 
> 
>     gtk_widget_show (listview)
> 
> }
> 
> This may display the contents of mysql_fetch_row on each row till it returns null, but I don't
> know how to add data and I can't find the answer (i'm sure it's on the tutorial but...) After, a
> click on  a button will remove the data and rebuild it (I hope the dynamic column creation is
> right...) Content will never excess 13 columns.
> 

Cheers,
                          -Tristan


More information about the Glade-users mailing list