Dynamically add columns to GTK4 ColumnView at runtime

55 Views Asked by At

What is the recommended pattern to dynamically add (or remove) columns to a GTK4 Column view at runtime?

Every example uses the following pattern:

  1. Create a GObject object to hold row data, with a fixed number of columns and predefined data types.
  2. Create column specific factories to setup widgets and callbacks for each column and bind them to the ListStore.

This pattern assumes that the number of columns and the datatype are known in advance, which may not always be the case.

This is a typical example in Python:

class DataObject(GObject.GObject):

    __gtype_name__ = 'DataObject'

    text = GObject.Property(type=str, default=None)
    number = GObject.Property(type=str, default=None)
    
    def __init__(self, text, number):
        super().__init__()
        self.text = text
        self.number = number
             
def setup_c1(widget, item):
    """Setup the widget to show in the Gtk.Listview"""
    cell = Gtk.EditableLabel()
    item.set_child(cell)
    cell.connect('changed', on_c1_change, item)

def bind_c1(widget, item):
    """bind data from the store object to the widget"""
    #item ListItem object
    label = item.get_child() #Gtk.EditableLabel
    obj = item.get_item() #DataObject
    label.set_text(obj.text)
    label.bind_property("text", obj, "text", GObject.BindingFlags.SYNC_CREATE)
        
def setup_c2(widget, item):
    """Setup the widget to show in the Gtk.Listview"""
    cell = Gtk.EditableLabel()    
    item.set_child(cell)
    cell.connect('changed', on_c2_change, item)

def bind_c2(widget, item):
    """bind data from the store object to the widget"""
    label = item.get_child() #Gtk.EdiableLabel
    obj = item.get_item() #DataObject
    label.set_text(obj.number)
    label.bind_property("text", obj, "number", GObject.BindingFlags.SYNC_CREATE)

0

There are 0 best solutions below