Listbox: programmatically scroll to row

I have a listbox and I’d like to have it programmatically scroll to a given row. So far I couldn’t find a working solution.

I’ve tried:


set_focus_vadjustment+ grab_focus on the child. I have tried to set the focus vadjustment on the listbox and on the viewport. And I’ve tried to call grab_focus on the ListBoxRow and on the child widget itself, to no avail. I saw that at least another user tried the same and failed too (link in a followup post because I don’t have the rights)


I’ve tried calling translate_coordinates between the ScrolledWindow and the ListBoxRow, and the Viewport and the ListBoxRow. And also between ScrolledWindow+ListBoxRow and the child widget itself. I got either 1 or -1 y coordinate difference every time, so not the correct value.

How can I achieve the desired effect with ListBox? Thank you!


Here’s the link to the other user who failed: :

so, I found the solution for my issue: since the grab_focus method didn’t work, I started implementing a workaround solution using listbox_get_row_at_y . Highly unsatisfying, but hopefully it was going to work. And… it didn’t work, because get_row_at_y would always return null , for all the y values I’d feed it. And I knew the listbox wasn’t empty. So that made me realize I was trying to focus a row that I had just been adding to the listbox… The row wasn’t realized yet, it couldn’t be focused because it wasn’t ready for that yet.

So I changed my code to fill the listbox, wait a 100ms timeout , and only then call grab_focus . And that worked!

I’m actually using a library which is wrapping the timeout call for me, but I think g_timeout_add in ‘raw’ gtk should work for that purpose.