Re: How to scroll to a specific line



On 07/26/2018 02:17 PM, John Coppens wrote:
get_iter_at_line, place_cursor, place_cursor_onscreen, and others. Even tried to
more or less calculate the scroll position and manipulate the vadjustments.

An example:
         itr = self.tbff.get_iter_at_line(linenr)
         self.tbff.place_cursor(itr)
         self.tview.place_cursor_onscreen()

I suspect that the problem may come from the fact that the widget has not
been rendered yet. I also tried to defer the actual scroll to GLib.idle_add,
but that didn't work out either.


This has worked for me:


        text_iter = self.source_buffer.get_iter_at_line (linenr)
        self.source_buffer.place_cursor(text_iter)
        self.source_view.scroll_to_iter(text_iter, 0.1, True, 0.0, 0.5)

I have also seen place_cursor_onscreen() work well. Now the thing is, none of this will work until the Gtk.SourceView has properly been rendered. This means doing something like:

        self.source_buffer.set_text("my text")
        GLib.idle_add(self.scroll_function)

    def scroll_function (self):
        # your scroll code here, like you use already

So you see, the scroll part is not the problem, but rather the calculations of iters fails, because these iters are not yet, or are invalidated when the buffer/view changes.

Cheers, Reuben




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