GtkScrolledWindow scrolling after change

I have a GtkScrolledWindow holding a GtkColumnView backed by a ListStore.

I have a button to add an entry to the bottom of the list (last entry). When I do this I want to programatically scroll the GtkScrolledWindow to the bottom (where the new row is). I’m using …

g_list_store_append ( listStore, calPoint );
GtkAdjustment *wScrollAdj = gtk_scrolled_window_get_vadjustment(  wScrolledWindow );
gtk_adjustment_set_value( wScrollAdj, gtk_adjustment_get_upper( wScrollAdj ) );

This doesn’t scroll to the bottom of the list but to the old bottom (before I added the row to the liststore).
Presumably the widgets have not had time to update to the expanded liststore. Can I trigger this before doing the scroll? How should this be handled?

Michael

Edit: I tried adding …

while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
  g_main_context_iteration (NULL, TRUE);

after adding the item to the list but then it didn’t scroll at all … somehow it doesn’t even get to that line 8-(

Hi,

The ColumnView size is not immediately recomputed when inserting a new element. It’s done asynchronously.

You can schedule the scroll later in idle. In C that would look like this:

{
   ...
   g_list_store_append ( listStore, calPoint );
   GtkAdjustment *wScrollAdj = gtk_scrolled_window_get_vadjustment(  wScrolledWindow );
   g_idle_add_once (do_scroll, wScrollAdj);
}

void
do_scroll (gpointer user_data)
{
    GtkAdjustment *wScrollAdj = (GtkAdjustment *)user_data;
    gtk_adjustment_set_value( wScrollAdj, gtk_adjustment_get_upper( wScrollAdj ) );
}
1 Like

I considered that method but wasn’t going to suggest that because it was a bit ugly (and I was sure I’d be ridiculed :scream_cat:).

Maybe look at Gtk.GridView.scroll_to , it may work without the idle stuff, but I never tried…

This is for a GtkGridView … not a GtkScrolledWindow - would that work?

woops… I meant Gtk.ColumnView.scroll_to

Yes, I see there seems to be some inherent scroll capability in GtkColumnView but I couldn’t get this to work. The widget just kept growing without scrolling. This is why I put it in a scrolled window (which does work as expected).

ColumnView, like ListView, must be packed into a ScrolledWindow; the scroll_to() methods rely on that as well.

OK, then yes gtk_column_view_scroll does scroll the column view when inside a GtkScrolledWindow but it suffers from the same issue. I still need to have an idle process to do the scroll because the added item in the ListStore has not propagated through to the widget.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.