StinkyCat
StinkyCat

Reputation: 1276

Add tooltip to GtkButton

I want to add a tooltip to a GtkButton.
This seems like an easy thing to do, except I have no idea why is not working. What I have:
1)

GtkButton* button = gtk_button_new_with_label("button");   
gtk_widget_set_tooltip_text(button, "tooltip text");

2)

GtkButton* button = gtk_button_new_with_label("button"); 
GtkTooltips *button_bar_tips = gtk_tooltips_new();
gtk_tooltips_set_tip(GTK_TOOLTIPS (button_bar_tips), 
                     button , "tooltip text", NULL);

Tried both with

gtk_widget_set_has_tooltip(button, true);

but still no luck. Any ideas? thks!

Upvotes: 3

Views: 5747

Answers (3)

angstyloop
angstyloop

Reputation: 327

I think since no GTK version was specified, GTK4 friends may look for a GTK4 example here.

Here is a GTK4 example that uses gtk_widget_set_tooltip_text.

Hover over the button to see the tooltip appear.

/** tooltip.c   
 *
 * COMPILE
 *
 * gcc `pkg-config --cflags gtk4` -o tooltip tooltip.c `pkg-config --libs gtk4`
 *
 * RUN
 *
 * ./tooltip
 */

#include <gtk/gtk.h>

// Not strictly necessary, but here for maximum portability.
#if GLIB_CHECK_VERSION(2, 74, 0)
static int app_flags = G_APPLICATION_DEFAULT_FLAGS;
#else
static int app_flags = G_APPLICATION_FLAGS_NONE;
#endif

static void activate( GtkApplication *app, gpointer user_data )
{
    GtkWidget *window, *button;

    window = gtk_application_window_new( app );

    // You can add tooltip text to any GtkWidget. I used a GtkButton.
    // You could just as easily use a GtkLabel by itself, or a GtkBox.
    button = gtk_button_new_with_label( "hover here" );

    gtk_widget_set_tooltip_text( button, "hello world" );

    gtk_window_set_child( GTK_WINDOW( window ), button );

    gtk_widget_show(window);
}

int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    puts("hover over the window to see the tooltip");

    app = gtk_application_new ("org.gtk.example", app_flags);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}

Upvotes: 0

LiuLang
LiuLang

Reputation: 773

You've mixed GtkWidget and GtkButton:

GtkButton* button = gtk_button_ne*emphasized text*w_with_label("button");   
gtk_widget_set_tooltip_text(button, "tooltip text");

see? gtk_widget_set_tooltip_text() expects that the first parameter is GtkWidget, not GtkButton, your compiler(GCC?) should have throw warning, like this:

tooltips.c:23:3: warning: passing argument 1 of ‘gtk_widget_set_tooltip_text’ from incompatible pointer type [enabled by default]*emphasized text*
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27:0,
                 from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
                 from /usr/include/gtk-3.0/gtk/gtkdialog.h:33,
                 from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
                 from /usr/include/gtk-3.0/gtk/gtk.h:31,
                 from tooltips.c:1:
/usr/include/gtk-3.0/gtk/gtkwidget.h:858:12: note: expected ‘struct GtkWidget *’ but argument is of type ‘struct GtkButton *’

Below is my example code:

#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
  GtkWidget *button, *window, *vbox;
  GtkButton *button2;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Tool Tip Test");
  gtk_widget_set_size_request (window, 200, 200);
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  g_signal_connect (G_OBJECT (window), "destroy",
      G_CALLBACK (gtk_main_quit), NULL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  button = gtk_button_new_with_label ("Click me");
  gtk_widget_set_tooltip_text (button, "Tooltip of button");
  gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 5);

  button2 = gtk_button_new_with_label ("No, click me");
  gtk_widget_set_tooltip_text (GTK_WIDGET (button2), "Tooltip of button2");
  gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (button2), TRUE, TRUE, 5);


  gtk_container_add (GTK_CONTAINER (window), vbox);

  gtk_widget_show_all (window);

  gtk_main ();
  return 0;
}

If you remove GTK_WIDGET() from:

  gtk_widget_set_tooltip_text (GTK_WIDGET (button2), "Tooltip of button2");
  gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (button2), TRUE, TRUE, 5);

My GCC will throw these warnings.

So it is a good way that ALL the widget varaibles in Gtk shall be inited as GtkWidget type, and then redefine them as other types when needed, using GTK_BUTTON(), GTK_CONTAINER, GTK_BOX(), GTK_WINDOW()...

Upvotes: 1

user2389519
user2389519

Reputation: 270

Well, GtkTooltips are deprecated now so let skip option 2), as for option 1) it looks completely right to me ...

So first, make sure you dont mix both approaches, maybe this is a problem...

Upvotes: 1

Related Questions