GTK4 - ColumnView - Get Selected Row When Left/Right Clicked On ColumnVİew

Previous row is get when GestureClick is used for GTK4 ColumnView.
What is the solution for getting the current (selected) row?
Also left button double click does not work.

Example code (source - edited):

import gi

gi.require_version("Adw", "1")
gi.require_version("Gtk", "4.0")

from gi.repository import Adw, Gio, GObject, Gtk  # noqa

class Country(GObject.Object):
    __gtype_name__ = "Country"

    def __init__(self, country_id, country_name, pm):

        self._country_id = country_id
        self._country_name = country_name
        self._country_pm = pm

    def country_id(self):
        return self._country_id

    def country_name(self):
        return self._country_name

    def country_pm(self):
        return self._country_pm

    def __repr__(self):
        return f"Country(country_id={self.country_id}, country_name={self.country_name})"  # noqa

class ExampleWindow(Gtk.ApplicationWindow):
    def __init__(self, app):
        super().__init__(application=app, title="Column View", default_width=300)

        nodes = {
            "au": ("Austria", "Van der Bellen"),
            "uk": ("United Kingdom", "Charles III"),
            "us": ("United States", "Biden"),

        self.model = Gio.ListStore(item_type=Country)
        for n in nodes.keys():
            self.model.append(Country(country_id=n, country_name=nodes[n][0], pm=nodes[n][1]))

        factory = Gtk.SignalListItemFactory()
        factory.connect("setup", self._on_factory_setup)
        factory.connect("bind", self._on_factory_bind, "country_name")
        factory.connect("unbind", self._on_factory_unbind, "country_name")
        factory.connect("teardown", self._on_factory_teardown)

        factory2 = Gtk.SignalListItemFactory()
        factory2.connect("setup", self._on_factory_setup)
        factory2.connect("bind", self._on_factory_bind, "country_pm")
        factory2.connect("unbind", self._on_factory_unbind, "country_pm")
        factory2.connect("teardown", self._on_factory_teardown) = Gtk.ColumnView()
        col1 = Gtk.ColumnViewColumn(title="Country", factory=factory)
        col1.props.expand = True
        col2 = Gtk.ColumnViewColumn(title="Head of State", factory=factory2)
        col2.props.expand = True = True = True

        tree_model =, False, True, self.model_func)
        tree_sorter =
        sorter_model = Gtk.SortListModel(model=tree_model, sorter=tree_sorter)
        self.selection =
        self.selection.connect('selection-changed', self.on_selection_changed)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12, valign=Gtk.Align.CENTER)


        event1 = Gtk.GestureClick()
        event1.connect("released", self.on_cv_pressed)
        event2 = Gtk.GestureClick()
        event2.connect("released", self.on_cv_pressed)

    def _on_factory_setup(self, factory, list_item):
        cell = Gtk.Inscription()
        cell._binding = None

    def _on_factory_bind(self, factory, list_item, what):
        cell = list_item.get_child()
        country = list_item.get_item().get_item()
        cell._binding = country.bind_property(what, cell, "text", GObject.BindingFlags.SYNC_CREATE)

    def _on_factory_unbind(self, factory, list_item, what):
        cell = list_item.get_child()
        if cell._binding:
            cell._binding = None

    def _on_factory_teardown(self, factory, list_item):
        cell = list_item.get_child()
        cell._binding = None

    def on_selection_changed(self, selection, position, n_items):

        country = selection.get_selected_item().get_item().country_name
        print("on_selection_changed: ", country)

    def model_func(self, item):

    def on_cv_pressed(self, event, count, x, y):
        country = self.selection.get_selected_item().get_item().country_name
        print("on_cv_pressed: ", country)
        if int(count) == 2:
            print("double click")

class ExampleApp(Adw.Application):
    def __init__(self):
        self.window = None

    def do_activate(self):
        if self.window is None:
            self.window = ExampleWindow(self)

app = ExampleApp()[])

You could get selected rows while clicking left button with set Gtk.ColumnView:single-click-activate

Activate rows on single click and select them on hover.

Rows are selected when mouse arrow is moved on them (even if there is no click).

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