Willits La réponse est 42 | Salut à tous,
Je débute en GTK+, et j'aimerai remplir un GTKTreeView avec le contenu d'un dossier.
Donc, j'ai suivie ce tuto: http://scentric.net/tutorial/, mais j'ai un petit soucis:
- Seul la première ligne de ma GtkListStore est affichée Voici mon code:
Code :
- extern GtkTreeModel * create_and_fill_model(char p_path[])
- {
- GtkListStore *liststore;
- GtkTreeIter toplevel;
- struct dirent *de = NULL;
- DIR *d = NULL;
- liststore = gtk_list_store_new(NUM_COLS,
- GDK_TYPE_PIXBUF,
- G_TYPE_STRING,
- G_TYPE_STRING);
- // Append a top level row and leave it empty
- gtk_list_store_append(liststore, &toplevel);
- // Reading the directory and filling the list store.
- d = opendir(p_path);
- if (d == NULL)
- return GTK_TREE_MODEL(liststore);
-
- // Loop while not NULL
- while (de = readdir(d))
- {
- gtk_list_store_set(liststore, &toplevel,
- COL_ICON, NULL,
- COL_FILENAME, de->d_name,
- COL_AUTH, "toto",
- -1);
- }
- // Closing the dir.
- closedir(d);
- return GTK_TREE_MODEL(liststore);
- }
|
Code :
- extern void create_view_and_model(GtkWidget * p_widget)
- {
- GtkTreeViewColumn *col;
- GtkCellRenderer *renderer;
- GtkTreeModel *model;
- /* --- Column #1 --- */
- col = gtk_tree_view_column_new();
- gtk_tree_view_column_set_title(col, "" );
- // pack tree view column into tree view
- gtk_tree_view_append_column(GTK_TREE_VIEW(p_widget), col);
- renderer = gtk_cell_renderer_pixbuf_new();
- // pack cell renderer into tree view column
- gtk_tree_view_column_pack_start(col, renderer, TRUE);
- // connect 'pixbuf' property of the cell renderer to
- // model column that contains the first name
- gtk_tree_view_column_add_attribute(col, renderer, "pixbuf", COL_ICON);
- /* --- Column #2 --- */
- col = gtk_tree_view_column_new();
- gtk_tree_view_column_set_title(col, "Name" );
- // pack tree view column into tree view
- gtk_tree_view_append_column(GTK_TREE_VIEW(p_widget), col);
- renderer = gtk_cell_renderer_text_new();
- // pack cell renderer into tree view column
- gtk_tree_view_column_pack_start(col, renderer, TRUE);
- // connect 'text' property of the cell renderer to
- // model column that contains the last name
- gtk_tree_view_column_add_attribute(col, renderer, "text", COL_FILENAME);
- // set 'weight' property of the cell renderer to
- // bold print (we want all last names in bold)
- g_object_set(renderer,
- "weight", PANGO_WEIGHT_BOLD,
- "weight-set", TRUE,
- NULL);
- /* --- Column #3 --- */
- col = gtk_tree_view_column_new();
- gtk_tree_view_column_set_title(col, "Rights" );
- // pack tree view column into tree view
- gtk_tree_view_append_column(GTK_TREE_VIEW(p_widget), col);
- renderer = gtk_cell_renderer_text_new();
- // pack cell renderer into tree view column
- gtk_tree_view_column_pack_start(col, renderer, TRUE);
- // connect 'text' property of the cell renderer to
- // model column that contains the last name
- gtk_tree_view_column_add_attribute(col, renderer, "text", COL_AUTH);
-
- // connect a cell data function
- //gtk_tree_view_column_set_cell_data_func(col, renderer, age_cell_data_func, NULL, NULL);
- model = create_and_fill_model("/" );
- gtk_tree_view_set_model(GTK_TREE_VIEW(p_widget), model);
- g_object_unref(model); /* destroy model automatically with view */
- gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(p_widget)),
- GTK_SELECTION_MULTIPLE);
- }
|
Merci, car j'ai beau regarder, je ne vois pas pourquoi il ne m'affiche qu'une seule ligne. (et google ne m'aide pas beaucoups...)
Willits
Edit: Résolu.
Avant de set une ligne, il faut la créer avec gtk_list_store_append(liststore, &toplevel);.
Cela donne donc:
Code :
- // Loop while not NULL
- while (de = readdir(d))
- {
- gtk_list_store_append(liststore, &toplevel); // Ne pas oublier ce truc.
- gtk_list_store_set(liststore, &toplevel,
- COL_ICON, NULL,
- COL_FILENAME, de->d_name,
- COL_AUTH, "toto",
- -1);
- }
|
Message édité par Willits le 16-09-2009 à 11:36:46
|