Re: GtkTreeView isn't updating when GtkListStore appended and set



Le 07/01/2014 22:48, Jordan H. a écrit :

Man, sorry, but I guess I'm still not getting this.

I've added a GtkTreeViewColumn and GtkCellRenderer to the GtkTreeView:

<object class="GtkTreeViewColumn" id="tvcol_button">
<property name="title" translatable="yes">Button Label</property>
<property name="clickable">True</property>
<property name="reorderable">True</property>
<child>
<object class="GtkCellRendererText" id="cr_button"/>
</child>
</object>

Actually the patch should look like this:

--- button-actions.glade
+++ button-actions.glade
@@ -122,6 +122,17 @@
                                 <property name="search_column">0</property>
                                 <property name="show_expanders">False</property>
                                 <property name="enable_grid_lines">horizontal</property>
+                                <child>
+                                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
+                                    <property name="title">Test</property>
+                                    <child>
+                                      <object class="GtkCellRendererText" id="cellrenderertext1"/>
+                                      <attributes>
+                                        <attribute name="text">0</attribute>
+                                      </attributes>
+                                    </child>
+                                  </object>
+                                </child>
                               </object>
                             </child>
                           </object>


[...]

    tree_view = GTK_TREE_VIEW( gtk_builder_get_object(builder,
        "treeview_button_order"));
    list_store = GTK_LIST_STORE( gtk_builder_get_object(builder,
        "liststore_button_list"));
    cell_renderer = GTK_CELL_RENDERER_TEXT(gtk_builder_get_object(builder,
        "cr_button"));
    column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder,
        "tvcol_button"));

    gtk_tree_view_column_pack_start(column,
        GTK_CELL_RENDERER(cell_renderer),
        TRUE);

why do you need to do all this?  Normally you add the column as a child
of the tree view so it works directly, no need to do it manually again.

    gtk_list_store_set(list_store, &iter, 0, last->title, -1);
    gtk_list_store_append(list_store, &iter);

This is wrong, you need to append to the store *before* setting the
values.  "iter" is only a mean of representing a position, nothing
holding your data.  So first, you append(), which sets the iter to the
newly added row, and then you set that row's data using set().

    g_object_set(G_OBJECT(cell_renderer), "text", last->title, NULL);

That's not correct.  The cell renderer object is used to *render* cells,
and only that.  The same will be used to render all rows, so setting the
property like that changes the one use to display all rows.  What you
should do is set up the column to set a particular property of the
renderer to the value of the cell it will render.

https://developer.gnome.org/gtk3/stable/GtkTreeViewColumn.html#gtk-tree-view-column-add-attribute

But if you had followed exactly what I told you, it should have been all
setup, to use the value of your "label" column.

    g_object_set(G_OBJECT(cell_renderer), "sensitive", TRUE, NULL);

   
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)),
                              GTK_SELECTION_NONE);
}

+++++++++++

But I get odd behavior when I add stuff to the list. I know for a fact
that last->title is being incremented (from "Button 1" to "Button 2",
etc.). However, when the GtkTreeView is updated I get ["Button 1"] on
the first click, ["Button 2", "Button 2"] on the second click, etc.

That's because you set the property on the cell renderer directly.  You
shouldn't do that for a property that should display a column's value.
Only do that for a property that should always be the same.

I've followed the example at
http://scentric.net/tutorial/sec-treeviewcol-renderer.html. From what I
can tell GtkCellRenderer controls the entire GtkTreeView (I think the
article says so, too). So then there doesn't seem to be any way to set
individual elements.

That's right.  To set individual elements, you bind a column to a cell
renderer through the TreeViewColumn.

Again, sorry for asking this question. Are GtkTreeViews supposed to be
difficult or am I just not understanding something?

They are not so simple, because they try to be generic MVC, that's right.



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