Thank you everybody for your work on GTK4.
I’m currently messing around with ListView and trying different applications for it.
Putting composite widgets in it etc.
I find it really nice overall, but I have a question regarding the sizing of ListViewItems:
Is it possible to have them non-homogeneous?
Minimum Working Example
#!/usr/bin/env python3
import time
import gi
gi.require_version(“Gtk”, “4.0”)
from gi.repository import Gtk, GLib, Gio, GObject
class Application:
def __init__(self):
self.app = Gtk.Application.new(None, 0)
self.app.connect("activate", self.activate)
def run(self):
self.app.run()
def activate(self, app):
self.win = MainWindow(app)
class MainWindow:
def __init__(self, app):
self.win = Gtk.ApplicationWindow.new(app)
self.win.connect("close-request", self.on_window_close_request)
self.win.set_title("Test ListView")
self.win.set_size_request(300, 200)
self.test_view = TestView()
self.win.set_child(self.test_view.get_widget())
self.win.show()
def on_window_close_request(self, window):
self.win.close()
class Entry(GObject.Object):
def __init__(self):
super().__init__()
def set_string(self, text):
self.text = text
def get_string(self):
return self.text
def create(self):
return None
class TestView:
def __init__(self):
self.sw = Gtk.ScrolledWindow()
self.sw.props.hscrollbar_policy = Gtk.PolicyType.NEVER
self.list_view = Gtk.ListView()
self.sw.set_child(self.list_view)
self.factory = Gtk.SignalListItemFactory()
self.factory.connect("setup", self.on_factory_setup)
self.factory.connect("bind", self.on_factory_bind)
self.list_view.set_factory(self.factory)
self.selection = Gtk.SingleSelection()
self.model = Gio.ListStore()
self.selection.set_model(self.model)
self.list_view.set_model(self.selection)
def get_widget(self):
return self.sw
def add(self, text):
obj = Entry()
obj.set_string(text)
self.model.append(obj)
def on_factory_setup(self, factory, listitem):
sw = Gtk.ScrolledWindow()
label = Gtk.Label()
sw.set_child(label)
listitem.set_child(sw)
listitem._label = label
def on_factory_bind(self, factory, listitem):
label = listitem._label
pos = listitem.get_position()
item = listitem.get_item()
text = item.get_string()
label.set_label(text)
sw = listitem.get_child()
label.props.xalign = 0
if pos % 2 == 0:
label.props.wrap = Gtk.WrapMode.WORD_CHAR
sw.props.hscrollbar_policy = Gtk.PolicyType.NEVER
else:
label.props.wrap = Gtk.WrapMode.NONE
sw.props.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC
def run_tests(app):
time.sleep(1)
text = "Jeder wackere Bayer vertilgt bequem zwo Pfund Kalbshaxen."
for i in range(1000):
app.win.test_view.add(text)
#time.sleep(2)
#app.win.win.close()
if name == “main”:
app = Application()
GLib.idle_add(run_tests, app)
try:
app.run()
except KeyboardInterrupt:
pass