1

Google says use ColumnView, because TreeView is deprecated. But how to create a UI like this with ColumnView of GTK4? Gnome Files seems to be able to do that, but I am not sure if they are using columnView. Could you show the bare minimum code to create this?

col1 col2
▾ level1 val1
    level2 val2

I can only find this in GTK4-demo, but this is definitely not I want. It is using two controls: one for the tree on the left, one for the table on the right.

strong text

0

1 Answer 1

3

I don't really know why, but many Gtk4 doc references don't talk about the new list view widget. The current way to make a treelist in Gtk4 is using Gtk.ListView and Gtk.TreeListModel.

You didn't say what programming language you're using, but here's a minimal python example.

Edited: I hadn't realized that the question was specifically about columns. In this case, the widget that should be used is really the Gtk.ColumnView. The example has been edited accordingly.

gi.require_version("Gtk", "4.0")
from gi.repository import Gtk, Gio, GObject  # noqa


class DataObject(GObject.GObject):
    def __init__(self, txt: str, txt2: str, children=None):
        super(DataObject, self).__init__()
        self.data = txt
        self.data2 = txt2
        self.children = children


def add_tree_node(item):
    
    if not (item):
            print("no item")
            return model
    else:        
        if type(item) == Gtk.TreeListRow:
            item = item.get_item()

            print("converteu")
            print(item)  
            
        if not item.children:
            return None
        store = Gio.ListStore.new(DataObject)
        for child in item.children:
            store.append(child)
        return store


def setup(widget, item):
    """Setup the widget to show in the Gtk.Listview"""
    label = Gtk.Label()
    expander = Gtk.TreeExpander.new()
    expander.set_child(label)
    item.set_child(expander)


def bind(widget, item):
    """bind data from the store object to the widget"""
    expander = item.get_child()
    label = expander.get_child()
    row = item.get_item()
    expander.set_list_row(row)
    obj = row.get_item()
    label.set_label(obj.data)
    
def setup1(widget, item):
    """Setup the widget to show in the Gtk.Listview"""
    label = Gtk.Label()    
    item.set_child(label)


def bind1(widget, item):
    """bind data from the store object to the widget"""
    label = item.get_child()
    row = item.get_item()
    obj = row.get_item()
    label.set_label(obj.data2)


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.ColumnView()  
    factory = Gtk.SignalListItemFactory()
    factory.connect("setup", setup)
    factory.connect("bind", bind)
    
    factory1 = Gtk.SignalListItemFactory()
    factory1.connect("setup", setup1)
    factory1.connect("bind", bind1)
    
    
    selection = Gtk.SingleSelection()
    
    store = Gio.ListStore.new(DataObject)
    
    model = Gtk.TreeListModel.new(store, False, False, add_tree_node)
    
    selection.set_model(model)
    
    list_view.set_model(selection)
    
    column1 = Gtk.ColumnViewColumn.new("colum01", factory)
    column2 = Gtk.ColumnViewColumn.new("colum02", factory1)
    
    list_view.append_column(column1)
    list_view.append_column(column2)
    
    v1 = [DataObject("entrada 01", "other")]
    v2 = [DataObject("entrada 02","", v1)]
    store.append(DataObject("entrada 03", "else", v2)) 

    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)
Sign up to request clarification or add additional context in comments.

1 Comment

I ran your code, but it only had one column. The UI I wanted (and described in my OP) has 2 columns (col1, col2). Could you modify the code two have two columns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.