I can’t seem to get my head around how GTK4 GListStore and GtkTreeListModel come together to provide an equivalent solution to what GtkTreeStore and GtkTreeView provided, specifically when dealing with child rows. I’ve read through List Widget Overview and have been looking at how the demo for the ListView Settings was put together. I see where the GtkTreeExpander comes into play with the display (view) side but I’m missing how I append child items to other items on the GListStore.
The ListView Settings example is interesting but it looks like the list is populated with keys and the GtkTreeListModelCreateModelFunc defined does a real-time lookup of children and returns a new GListStore – effectively creating new lists instead of having one list. My C is a novice level so I may have missed what’s really going on there.
With GtkTreeStore, I would append a child to the store by including the iter of the parent. So, with that thought in mind, I expected to see an ability to append a child to a parent in some manner via the GtkTreeListModel and then it would manage the tagging of the row in some internal mechanism to indicate the relationship, flattening the model so that it could be stored in the GListStore. And then the GtkTreeListRow would recognize parent/child/grandchild relationships to provide proper organization for presentation. Effectively, the GtkTreeListModel being the interface between a tree and the storage of the tree. But GtkTreeListModel does not have append functions so it appears that GtkTreeListModel is intended to provide the interface for the view operation (and sort and filter). That leaves me with how to populate the GListStore with both parent and child rows, properly linked to support the view with expanders.
I did manage to successfully populate a GListStore with multiple GObjects that represent the different logical rows. For instance:
class SideBarGroupRow(GObject.GObject):
sortval: int
group: str
favorite: bool
‘’’ For Trees and Tree models, you need to create your own custom data objects based on Gobject ‘’’def __init__(self, sortval: int, group: str, favorite: bool): super().__init__() self.sortval = sortval self.group = group self.favorite = favorite def __repr__(self): return f'SideBarRow(sortval: {self.sortval} group: {self.group} favorite: {self.favorite})'
class SideBarAccountRow(GObject.GObject):
group: str
name: str
favorite: bool
isVisible: bool
tableName: str
itemID: int
acctType: str
‘’’ For Trees and Tree models, you need to create your own custom data objects based on Gobject ‘’’def __init__(self, group: str, name: str, favorite: bool, isVisible: bool, tableName: str, itemID: int, acctType: str): super().__init__() self.group = group self.name = name self.favorite = favorite self.isVisible = isVisible self.tableName = tableName self.itemID = itemID self.acctType = acctType def __repr__(self): return f'SideBarRow(group: {self.group} name: {self.name} favorite: {self.favorite} isVisible: {self.isVisible} tableName: {self.tableName} itemID: {self.itemID} acctType: {self.acctType})'
And then simple appends:
sideBarList = Gio.ListStore() # Group/Category Row sideBarList.append(SideBarGroupRow(None, groupName, False)) # Child Accounts sideBarList.append(SideBarAccountRow(acctName, None, True, True, 'account', acctId, acctType))
But with this approach, I haven’t seen how I would connect the GtkTreeListModel and its GtkTreeListModelCreateModelFunc to that list. I have a list of GObjects, but need a way to connect them into the TreeListModel for sorting, filtering, expander, selection, and view purposes. That’s telling me that I’m likely headed down the wrong path and am not understanding the intended pattern to be used to move away from a TreeStore and handle child rows.
Any and all thoughts, pointers, examples, links… are much appreciated.