Small Update
Optimization 3
My table has a right-click context menu. Because the content and actions of this context menu depends on the cell value, I defined this menu as part of this cell widget. This leads to one separate menu instance for each cell, i.e., a lot of redundant initialization.
The mitigation for this is easy: define the menu once within the column view. However, since the content and behavior of this menu depends on the clicked cell value, this implementation isn’t very straight-forward, to the best of my knowledge, GTK4 don’t provide any straight-forward methods for this and I don’t know any open-source GTK4 application that I could use as a reference. The only resource I’ve found is this forum post that misses a conclusion.
So, I came up with my own solution. It’s a bit hacky for my taste and I’m not proud of it, so I welcome anyone with a better solution. I subclass Gtk.PopoverMenu and add a mutable GObject.Property named selected_content. The column view now contains an instance of this popover menu as a child and submits this instance as constructor argument to each of the cell widgets within the setup_column callback method. The cell widget stores the reference internally and when registering a right click or long press, it sets the selected_content property of the popover menu, then sets its coordinates as follows:
column_view = self._popover_menu.get_parent()
position = Gdk.Rectangle()
position.x, position.y = self.translate_coordinates(column_view, x, y)
self._popover_menu.set_pointing_to(position)
The translate_coordinates is important because x and y are relative to the internal coordinate system of the cell widget and must be translated to the coordinate system of the column view. Otherwise the popover menu will always appear at the top left of the column view.
It finally calls popover_menu.popup() within the event handler.
This reduces the initialization time by a few hundred milliseconds from 1100 ms to about 730 ms.
Optimization 4
Since my cells can contain different kinds of data with different representations, I’m currently using a Gtk.Stack and set the stack page according to the data type. However, the application has to create 4 times the number of elements it actually needs, so it takes a toll on the performance. However, most cells only contain raw text, while only a few cells contain a special kind of data, so it makes sense to always instantiate the text label in setup and only instantiate the other three cell widgets on demand in bind.
I implemented this with properties that, when accessed, lazily create and add the widget before returning it.
As a consequence, I can reduce the row initialization time from about 730 ms to about 350 ms.