0

This is the relevant part of the code:

 btn1 = gtk_button_new_with_label("one");
 gtk_grid_attach (GTK_GRID (grid_ptr), btn1, 0, 1, 1, 1);
 gtk_widget_set_name ( btn1, "btn1");
   
 GtkCssProvider *prvdr = gtk_css_provider_new ();
 GtkStyleContext *cntxt = gtk_widget_get_style_context ( btn1 );
    
 gtk_css_provider_load_from_data (prvdr, "button#btn1 { text-decoration: underline white;}", -1);
 gtk_style_context_add_provider (cntxt, GTK_STYLE_PROVIDER (prvdr), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

It compiles without any issues, and runs without errors or warnings, but nothing gets underlined.

I have also tried these with the same result: "button#btn1 { text-decoration: underline;}" "button#btn1 { text-decoration-line: underline;}"

I have changed the text colour using "button#btn1 { color: #00ff00;}".

Can anyone explain why it does not work, or how to fix it?

1
  • text-decoration is not available on buttons in gtk4. A possible fix could be styling the label instead? Commented Jul 2, 2023 at 17:36

1 Answer 1

0

For this to work, you shall have to use CSS selector for the label inside the button. Say something like button#btn1 > label {...}

A minimal example for the same:

/* button.c
 *
 * Compile: cc -ggdb button.c -o button $(pkg-config --cflags --libs gtk4)
 * Run: ./button
 *
 * Author: Mohammed Sadiq <www.sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
 */

#include <gtk/gtk.h>

static void
app_activated_cb (GtkApplication *app)
{
  g_autoptr(GtkCssProvider) css_provider = NULL;
  GtkWindow *window;
  GtkWidget *button;

  button = gtk_button_new_with_label ("Click me!");
  gtk_widget_set_name (button, "main");
  css_provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (css_provider,
                                   "button#main > label { text-decoration: underline white;}", -1);
  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
                                              GTK_STYLE_PROVIDER (css_provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  window = GTK_WINDOW (gtk_application_window_new (app));
  gtk_window_set_default_size (window, 600,400);
  gtk_window_set_child (window, button);

  gtk_window_present (window);
}

int
main (int   argc,
      char *argv[])
{
  g_autoptr(GtkApplication) app = gtk_application_new (NULL, 0);

  g_signal_connect (app, "activate", G_CALLBACK (app_activated_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mohammed, your solution works. For anyone else reading this I found I needed to make two changes to my code. I changed the css text to "button#btn1 > label { text-decoration: underline white;}", and had to use gtk_style_context_add_provider_for_display() instead of gtk_style_context_add_provider(). I do not know why the function needed changing; from the documentation it looks like both methods should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.