ListBox won't scroll to new row when grabbing focus

Hi,

I have an application with a GtkListBox and I would like to add a new child and focus to it such that the view also scrolls to the focused child. Just calling grab_focus() on the child directly after adding it will reset the scroll position to the top which is highly annoying. From here I found out that this is due to the ListBoxRow not being realized yet and indeed calling grab_focus() after a small timeout works, but I’m wondering if there is a more “correct” solution.

I’ve already tried calling grab_focus() when the GtkListBoxRow or the child emit realize, but that doesn’t fix the bug with the scrolling.

Cheers!

Try this before grap_focus

context = GLib.MainContext().default()
while context.pending():
    context.iteration(True)

And look at this example (on_add_button_clicked).
https://github.com/yucefsourani/python-gtk4-examples/tree/main/simple_list_view_example

Thanks!

This mostly works, but unfortunately it’s not quite as reliable as the manual timeout, i.e. the scrolling sometimes (but rarely) jumps to the top of the list.

1 Like