我正在尝试使用GTK3动态更改窗口的光标,但gtk_widget_get_parent_window
似乎不起作用。
有人可以指出我做错了什么吗?谢谢!
// https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
// minimal example
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
// Here \/\/\/\/\/ .
GdkWindow* w = gtk_widget_get_parent_window(window);
GdkCursor* c = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_WATCH);
gdk_window_set_cursor(w, c);
// /\/\/\/\/\ .
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
(main.exe:16508):Gdk-CRITICAL **:gdk_window_set_cursor:断言 'GDK_IS_WINDOW(窗口)'失败
我正在使用带有msys2的GTK 3.16
非常感谢提前。
答案 0 :(得分:1)
扩展@andlabs评论
在将窗口小部件添加到窗口小部件层次结构或GTK术语realized
之后,需要尝试更改光标。
在为窗口小部件触发gtk_widget_get_parent_window()
事件之前调用gtk_widget_get_window()
甚至realize
将导致两种情况下都出现NULL指针。
与@andlabs一样,将gtk_widget_get_window()
与GtkWindow
结合使用会更安全。
解决方案。
static GdkWindow* G_WINDOW = 0;
static GdkCursor* G_CURSOR = 0;
// call after WindowRealize()
void changecursor()
{
assert(G_WINDOW != NULL);
gdk_window_set_cursor(G_WINDOW, G_CURSOR);
}
static void WindowRealize(GtkWidget *window, gpointer data)
{
G_CURSOR_HAND = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_HAND2);
G_WINDOW = gtk_widget_get_window(window);
}
static void activate(GtkApplication* app,gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
...
g_signal_connect(window, "realize", G_CALLBACK(WindowRealize), NULL);
gtk_widget_show_all (window);
}