你已经混合了 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() 将它们重新定义为其他类型是一个好方法...