2

我想向 GtkButton 添加一个工具提示。
这似乎是一件容易的事,但我不知道为什么不工作。我有什么:
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);

都试过了

gtk_widget_set_has_tooltip(button, true);

但仍然没有运气。有任何想法吗?谢谢!

4

2 回答 2

1

好吧,GtkTooltips 现在已被弃用,所以让我们跳过选项 2),至于选项 1)它对我来说看起来完全正确......

所以首先,确保你不要混合两种方法,也许这是一个问题......

于 2013-05-16T12:20:37.743 回答
1

你已经混合了 GtkWidget 和 GtkButton:

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

看?gtk_widget_set_tooltip_text() 期望第一个参数是 GtkWidget,而不是 GtkButton,你的编译器(GCC?)应该有抛出警告,像这样:

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 *’

下面是我的示例代码:

#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;
}

如果您从以下位置删除 GTK_WIDGET():

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

我的 GCC 会抛出这些警告。

因此,将 Gtk 中的所有小部件变量都初始化为 GtkWidget 类型,然后在需要时使用 GTK_BUTTON()、GTK_CONTAINER、GTK_BOX()、GTK_WINDOW() 将它们重新定义为其他类型是一个好方法...

于 2013-05-16T16:48:27.707 回答