Gtk.TextView like row of Gtk.ListView does not expand

I’m using Gtk.TextView as the row of a Gtk.ListView and the Gtk.TextView does not expand to the size of the text it contains until one of the rows in the Gtk.ListView is clicked.

Using Gtk.Widget_set_size_request() guarantees that the Gtk.TextView expands to a certain size, but it does not solve the problem because I have no way of knowing what size is necessary to accommodate the text contained in the Gtk.TextBuffer.

Adding the Gtk.TextView to a Gtk.Box generates other problems, such as the fact that clicking on the Gtk.TextView does not work to edit it (a double click is necessary).

I’ve already tried several things and at the moment I’m trying to understand what exactly clicking on a row does, and why this action causes expansion.

Here is a minimal example:

import gi
gi.require_version("Gtk", "4.0")

from gi.repository import Gtk, Gio, GObject


class DataObject(GObject.GObject):

    __gtype_name__ = 'DataObject'

    def __init__(self, text):

        super().__init__()

        self.buffer = Gtk.TextBuffer.new()
        self.buffer.set_text(text)


def setup(widget, item):
    """Setup the widget to show in the Gtk.Listview"""
    view = Gtk.TextView.new()
    view.set_wrap_mode(2)
    view.set_vexpand(True)

    item.set_child(view)


def bind(widget, item,):
    """bind data from the store object to the widget"""
    view = item.get_child()
    obj = item.get_item()

    view.set_buffer(obj.buffer)
    view.set_size_request(-1, 20)

    iter = obj.buffer.get_start_iter()
    obj.buffer.place_cursor(iter)
    view.grab_focus()


def on_activate(app):
    win = Gtk.ApplicationWindow(
        application=app,
        title="Gtk4 is Awesome !!!",
        default_height=400,
        default_width=400,
    )
    sw = Gtk.ScrolledWindow()
    list_view = Gtk.ListView()
    store = Gio.ListStore.new(DataObject)

    selection = Gtk.NoSelection()
    selection.set_model(store)

    list_view.set_model(selection)

    factory = Gtk.SignalListItemFactory()
    factory.connect("setup", setup)
    factory.connect("bind", bind)

    list_view.set_factory(factory)

    text = "The size request of a widget is the smallest size a widget can"\
        " accept while still functioning well and drawing itself correctly."\
        " However in some strange cases a widget may be allocated less than "\
        "its requested size, and in many cases a widget may be allocated more"\
        " space than it requested. if the size request in a given direction "\
        "is -1 (unset), then the natural size request of the widget will be"\
        " used instead."

    v1 = DataObject(text)
    v2 = DataObject("two")
    v3 = DataObject("three")

    store.append(v1)
    store.append(v2)
    store.append(v3)

    sw.set_child(list_view)
    win.set_child(sw)
    win.present()


app = Gtk.Application(application_id="org.gtk.Example")
app.connect("activate", on_activate)
app.run(None)

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