cpython-3.xgtkpygobjectgtk4

How to set background colour for row in Gtk.ColumnView


I am trying to set the background colour for some Gtk.ColumnView rows in Gtk 4. I've managed to set custom styling on normal widgets using:

cssProvider = Gtk.CssProvider()
cssProvider.load_from_file(Gio.File.new_for_path("/path/to/css/file.css"))
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), cssProvider, Gtk.STYLE_PROVIDER_PRIORITY_USER)

mywidget.add_css_class("custom-widget")

However, I can't seem to get this to work on a columnview row. Running add_css_class on the item passed by Gtk.SignalListItemFactory returns AttributeError: 'TreeListRow' object has no attribute 'add_css_class'.

I'm using PyGObject but any thoughts or code in C/C++/rust/etc. would still be much appreciated.


Solution

  • The error states the object "mywidget" is of type TreeListRow, which is not a widget, but rather something like a wrapper around objects that are grouped as a "row". TreeListRow is derived from GObject: https://docs.gtk.org/gtk4/class.TreeListRow.html . Neither GObject, nor TreeListRow have such css methods.

    Try applying the set_css_classes() or add_css_class() to the GTK widget(s) that is/are contained within the TreeListRow.