c++gtk3linux-mint

How to add an image to a button with GTK


I am trying to add an image to a button with label, but the image doesn't show and the broken image doesn't display either.

stop_button = gtk_button_new_with_label("stop");
image = gtk_image_new_from_file ("/home/cendit/Escritorio/index.jpeg");
gtk_button_set_image (GTK_BUTTON(stop_button),image);

I tried a different path "file:///home/cendit/Escritorio/index.jpeg" but it was not successful.


Solution

  • This is what yo need to do

    GtkImage *imagen_pantalla_completa;
    GtkWidget *pantalla_completa;
    
    pantalla_completa = gtk_button_new_with_label("");                                                  
    imagen_pantalla_completa = (GtkImage *)gtk_image_new_from_file("/home/user..."); 
    gtk_button_set_image (GTK_BUTTON(pantalla_completa),(GtkWidget *)imagen_pantalla_completa); 
    

    You need to add this to display the image

    GtkSettings *default_settings = gtk_settings_get_default();
    g_object_set(default_settings, "gtk-button-images", TRUE, NULL);