pythongtkgtktreeviewgtkscrolledwindow

Treeview jumps to top on selecting or editing item


In my GTK3 window I have a Treeview with many items, so I have make it scrollable. I'm using a Scrolledwindow for that:

self.codes = Gtk.Treeview()
codeswindow = Gtk.ScrolledWindow()
codeswindow.add_with_viewport(self.codes)
codeswindow.show()

When running the application and selecting an item ("selection_changed" event) the Treeview will jump to the top, so the selected item will be out of sight. The same happens when double clicking an item for editing it. After selecting an item I then have to scroll down to the selected item.

How do I prevent this?

Maybe related: When selecting items using Arrow up or Arrow down the window is not scrolling along.


Solution

  • Like suggested in the comment by @Aran-Fey Treeview can scroll natively and quote from documentation:

    If a child has native scrolling, use gtk_container_add() instead of this function

    and by the way:

    gtk_scrolled_window_add_with_viewport has been deprecated since version 3.8 and should not be used in newly-written code.

    Replacing add_with_viewport with add solves the problem:

    self.codes = Gtk.Treeview()
    codeswindow = Gtk.ScrolledWindow()
    codeswindow.add(self.codes)