I’m currently using GtkCssProvider in my Gtk4 app but it’s giving a warning that it’s deprecated and will be removed in Gtk5. Is there a new mechanism for loading CSS styles for a widget?
I was wondering the same thing. I guess you’re supposed to set CSS classes on your widget and use a global stylesheet instead of a per-widget stylesheet.
GtkCSSProvider is not deprecated; you may be confusing it with GtkStyleContext.
Yes, setting a style provider on a single widget is deprecated; but it also never actually worked the way people thought it did: per-widget style providers never cascaded into the children of the widget, which meant you had to add a bunch of style providers across the widget hierarchy to achieve the same result as adding a global style provider to the display.
Add a style provider to the display, define classes in your CSS, and then add/remove them from widgets as needed.
I got it working by adding the CSS file to gresources:
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/ca/footeware/c/texty">
<file preprocess="xml-stripblanks">texty-window.ui</file>
<file preprocess="xml-stripblanks">gtk/help-overlay.ui</file>
<file>main.css</file>
</gresource>
Then accessing it using:
GtkCssProvider *cssProvider;
cssProvider = gtk_css_provider_new();
gtk_widget_set_name (GTK_WIDGET(self->main_text_view), "text-view");
gtk_css_provider_load_from_resource (cssProvider, "/ca/footeware/c/texty/main.css");
gtk_style_context_add_provider_for_display(
gdk_display_get_default(),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
main.css:
#text-view {
font-size: 24px;
}
No more deprecated warning,
That’s indeed the way it’s meant to work. A nitpick: use CSS classes, instead of widget names. There is no expectation of uniqueness for widget names, and you want to be able to take advantage of different specificities when using CSS selectors.
AFAIK adding a style provider to the display using gtk_style_context_add_provider_for_display
will always result in a deprecation warning (at least in my java binding) because it is a function of the StyleContext class, which is deprecated.
That’s a bindings issue: the function is not deprecated, because it does not take a GtkStyleContext
instance.
The Rust bindings have solved the namespacing issue by introducing a static function called gtk::style_context_add_provider_for_display()
.
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.