0

I try to create a TreeView in GTK4 using python. I managed to create columns which show up correctly - the generated rows also show up but they show no values as if there was no CellRenderer specified... enter image description here

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

...

colA = Gtk.TreeViewColumn("Title A", Gtk.CellRendererText())
colB = Gtk.TreeViewColumn("Title B", Gtk.CellRendererText())

self.treeStore = Gtk.TreeStore(str, str)
self.tree = Gtk.TreeView.new_with_model(self.treeStore)
self.tree.append_column(colA)
self.tree.append_column(colB)

for i in range(0, 10):
    self.treeStore.append(None, [str(i), "Hi"])

How can i make the values appear?

Full example

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

class Example(Gtk.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app, title="Example TreeView")
        self.win.present()


class MainWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_default_size(600, 400)

        colA = Gtk.TreeViewColumn("Title A", Gtk.CellRendererText())
        colB = Gtk.TreeViewColumn("Title B", Gtk.CellRendererText())

        self.treeStore = Gtk.TreeStore(str, str)
        self.tree = Gtk.TreeView.new_with_model(self.treeStore)
        self.tree.append_column(colA)
        self.tree.append_column(colB)

        for i in range(0, 10):
            self.treeStore.append(None, [str(i), "Hi"])
        
        self.set_child(self.tree)


app = Example()
app.run()
jarinox
  • 45
  • 7

1 Answers1

0

I managed to make it work with a ListStore:

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

class Example(Gtk.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app, title="Example TreeView")
        self.win.present()


class MainWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_default_size(600, 400)

        self.list_store = Gtk.ListStore(str, str)
        for i in range(0, 10):
            self.list_store.insert_with_values(i, (0, 1), (i, "Hi"))

        self.tree = Gtk.TreeView.new_with_model(self.list_store)

        colA = Gtk.TreeViewColumn("Title A", Gtk.CellRendererText(), text=0)
        colB = Gtk.TreeViewColumn("Title B", Gtk.CellRendererText(), text=1)
        self.tree.append_column(colA)
        self.tree.append_column(colB)


        self.set_child(self.tree)

app = Example()
app.run()
Gonzalo Odiard
  • 1,238
  • 12
  • 19