How to access row item of ListView?

Hi! I need to add a custom class to some rows in a GtkListView widget so that I can change their background colour. Looking on the inspector, ListItem rows are instances of GtkListItemWidget, but I can’t find anything in the docs or online regarding how to access these widgets. I’m using GTK 4.

Hopefully I’m using the right tags…first time user of discourse. Would anyone have any insights on how to access ListItem rows?

Thanks in advance!

There are several ways to access a widget that represents an item in a Gtk.ListView, it all depends on what you are trying to do. Are you using the factory setup and bind functions? Can this class be added in setup or bind?

Once inserted, the widgets that represent an item can be accessed through Gtk.Selection if you have used SingleSelection or Multiselection. If you chose NoSelection there is no way to access them externally (that I know of), but you can create a reference to the item somewhere that you can access later during the bind or setup phase.

ListViews are a little complicated at first, but once you understand all the elements they become less daunting.


Under which condition do you want to change the class?

That would be interesting to know, because maybe you get a callback with a reference to the widget, in which case you can avoid involving the ListView and model.

Hi! To answer both questions, I am using SingleSelection and SignalListItemFactory. On setup, I am trying to add a class to some of the list rows being created to change their background color.

In setup, the widget you create is the widget that will appear in the list, that is, if you change its background in the setup function it will appear like this in the list. What, exactly, isn’t working?

I have no issues changing the background of the widget I create, but I need to change the background of the list row itself that my widget is in.

I need to change the row itself because they have curved borders and if I change the background of my widget instead of the row it starts to look very wonky.

There’s got to be a way…

Hm I did a cdrhandler before but using gtkcolumnview…I thing the logic should be the same.

  1. you need a factory
    factory = gtk_signal_list_item_factory_new();

  2. you need to connect to “setup” and “bind” signal
    “signal::setup”, G_CALLBACK(test_setup), (gpointer)priv->store,
    “signal::bind”, G_CALLBACK(test_bind), (gpointer)priv->store,

in the “setup” handler you define the widget to show informations from your list
in the “bind” signal you define how the data is applied to the widget so that it is displayed…

the class can be setted in “setup” handler or in the “bind” handler…doign this in the “bind” handler suppose that an item (of the row) from you model influence the background of the widget used to show the relevant row.

Hope this give you some ideas…

1 Like

You can’t access a ListView’s ListItemWidgets. Though by the smell of it, this seems to be caused by the padding added to ListItemWidgets by default.

You can try removing it with some css along the lines of:

row {
  padding: 0;

Maybe that’ll do it.

If not, a picture demonstarting the problem you are having, would be useful.

I ran into this same problem recently when I was building a widget to use as an icon in drag and drop. Changing a list row background is a bad idea for several reasons, it’s easier for you to adapt the background of your widget to also have rounded edges, just add padding and border-radius.

padding: 8px and border-radius: 5px is a good starting point.

Well, that explains it. I was hoping to not have to set the border-radius and padding myself because these might depend on the theme that the user has installed, but it sounds like it is going to the best way to do this.

Thank you all!