GtkToolButton's "use-underline" property does not get set

I created a new Item with gtk_tool_button_new() for GtkToolbar and after I set everything done I noticed that the “use-underline” property does not work.

Neither with g_object_set() nor with gtk_tool_button_set_use_underline()

Here is an example Code:

#include <gtk/gtk.h>

void call_callback ( GtkToolButton *toolButton );

int main ( void )
{
    GtkWidget   *window;
    GtkWidget   *box;
    GtkWidget   *toolBar;
    GtkToolItem *toolItem;

    /// ***
    gtk_init ( NULL, NULL );

    /// *** Create a Window
    window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
    g_signal_connect ( window, "delete-event", gtk_main_quit, NULL );
    g_signal_connect ( window, "destroy",      gtk_main_quit, NULL );

    /// *** Create a Box
    box = gtk_box_new ( GTK_ORIENTATION_VERTICAL, 5 );
    gtk_container_add ( GTK_CONTAINER ( window ), box );

    /// *** Create a ToolBar
    toolBar = gtk_toolbar_new();
    gtk_widget_set_name ( GTK_WIDGET ( toolBar ), "CallItem" );
    gtk_toolbar_set_style ( GTK_TOOLBAR ( toolBar ), GTK_TOOLBAR_BOTH );

    /// *** Create a ToolButton
    toolItem  = gtk_tool_button_new  ( gtk_image_new_from_icon_name ( "call-start",    GTK_ICON_SIZE_SMALL_TOOLBAR ), "_Call" );
    gtk_widget_set_tooltip_text ( GTK_WIDGET ( toolItem ),  "Make a Call" );
    gtk_toolbar_insert ( GTK_TOOLBAR ( toolBar ), toolItem, 0 );

    /// *** Monitor the CLICKED signal
    g_signal_connect_swapped ( toolItem,  "clicked", G_CALLBACK ( call_callback ), toolItem );

    /// *** Add it to the Box
    gtk_box_pack_start ( GTK_BOX ( box ), toolBar, 0, 0, 0 );
    gtk_widget_set_halign ( toolBar, GTK_ALIGN_CENTER );
    gtk_orientable_set_orientation ( GTK_ORIENTABLE ( toolBar ), GTK_ORIENTATION_HORIZONTAL );

    /// *** Set use underline to TRUE
    ///gtk_tool_button_set_use_underline ( GTK_TOOL_BUTTON ( toolItem ), TRUE );
    g_object_set( toolItem, "use-underline", TRUE, NULL );
    /// ***
    gtk_widget_show_all ( window );
    gtk_main ();
}

void call_callback ( GtkToolButton *toolButton )
{
    g_return_if_fail ( GTK_IS_TOOL_BUTTON ( toolButton ) );
    g_print ( "The Call button was Clicked\n" );
}

Do I need to set it somehow different for the mnemonic accelerator key?

The documentation says that “Labels shown on tool buttons never have mnemonics on them; this property only affects the menu item on the overflow menu.” I don’t really find the comment in the code clearer, but I think it confirms you have no hopes. You probably should modernize to other/newer witgets.

1 Like

I am not sure if the problem is only there. If I use the Inspector everything works fine:
Please watch this Video.

I do know that works with (or something similar):

GtkWidget *label = gtk_label_new_with_mnemonic( "_Call" );
toolItem  = gtk_tool_button_new  ( label, NULL

But I do not understand why it does work with the Inspector in that case.

Because in your video you are setting use-underline on the GtkLabel, not the GtkToolButton. If you want to do that programmatically, you should just pull the widget and set it directly.

1 Like

Yes I noticed also that.
Anyway the Label-Widget Property solved the problem.

1 Like