Add custom CSS when no GdkDisplay is returned

I’m trying to add custom CSS to my app. I followed what is suggested in this tutorial (adapting screen to display, because I’m using GTK4), but it does not work because the gdk_display_get_default is consistently returning NULL.

I tried to list all the displays, but I get an empty list:

  GSList * displays = gdk_display_manager_list_displays(gdk_display_manager_get ());
  guint length = g_slist_length (displays);
  g_print("length = %u\ndisplays =\n", length);

  guint i;

  for(i = 0; i < length; ++i) {
    g_print("\t%p\n", g_slist_nth (displays, i));


length = 0
displays =

So, is there another way to add custom CSS?

Where in your app is this code running?

I was running it from inside my custom init function (I extend from GtkApplication) and it was not working… I moved it to the activate and it worked.

Sorry, my bad…

The default display connection is available after the toolkit has been initialised.

The proper place to add a custom CSS provider is in GApplication::startup; you do not want to use activate, as that will be invoked every time your application gets activated—e.g. if a new instance gets launched when one is already running.

1 Like