Stop using GtkWidget event signals

Hello,

I’m trying to start cleaning Lollypop code to prepare GTK4 migration but failed to use Gtk.EventControllerScroll to replace a connect on “scroll-event” signal, does not work here.

What is wrong with this code? I tried creating controller on listbox without success.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class View:
    def __init__(self):
        win = Gtk.Window()
        win.connect("destroy", Gtk.main_quit)
        scrolled = Gtk.ScrolledWindow()
        win.add(scrolled)
        listbox = Gtk.ListBox()
        scrolled.add(listbox)
        for i in range(0, 100):
            label = Gtk.Label.new(str(i))
            listbox.add(label)
        self.controller = Gtk.EventControllerScroll.new(scrolled, Gtk.EventControllerScrollFlags.BOTH_AXES)
        self.controller.connect("scroll", self.on_scroll)
        win.show_all()

    def on_scroll(self, controller, dx, dy):
        print(dx, dy)

if __name__ == "__main__":
    View()
    Gtk.main()

The recommendation to use gestures is mostly meant for your own custom widgets, not for existing GTK widgets.

GtkScrolledWindow is consuming those scroll events already, so your gesture won’t see them. Additionally, GtkScrolledWindow in GTK 3 is not using a scroll gesture.

The question is: what are you trying to achieve? If you want to be notified that the scrolled window is scrolling, you should use the adjustment’s values, instead.

You are right, will try to remove this horrible hack instead: https://gitlab.gnome.org/World/lollypop/issues/1803

So here my question, I have an overlay widget with:

  • a scrolled window as main widget
  • another clickable widget aligned on right (added with add_overlay)

How can I keep right widget clickable and passing scroll event to main scrolled window?

Should be simpler with an example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import string

class View:
    def __init__(self):
        win = Gtk.Window()
        win.connect("destroy", Gtk.main_quit)
        self.char_grid = Gtk.Grid()
        self.char_grid.set_orientation(Gtk.Orientation.VERTICAL)
        char_scrolled = Gtk.ScrolledWindow()
        char_scrolled.set_property("halign", Gtk.Align.END)
        char_scrolled.get_vscrollbar().hide()
        char_scrolled.set_policy(Gtk.PolicyType.NEVER,
                                 Gtk.PolicyType.EXTERNAL)
        char_scrolled.add(self.char_grid)
        self.listbox = Gtk.ListBox()
        scrolled = Gtk.ScrolledWindow()
        scrolled.add(self.listbox)
        overlay = Gtk.Overlay()
        overlay.add(scrolled)
        overlay.add_overlay(char_scrolled)
        self.add_letters()
        self.add_scrolled_content()
        win.add(overlay)
        win.show_all()

    def add_scrolled_content(self):
        for i in range(0, 100):
            label = Gtk.Label.new(str(i))
            self.listbox.add(label)

    def add_letters(self):
        for i in string.printable:
            label = Gtk.Label.new(i)
            self.char_grid.add(label)

if __name__ == "__main__":
    View()
    Gtk.main()

I want char_scrolled to scroll and when at max/min, pass event to parent scrolled window.

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