gpt4 book ai didi

c - (堆栈 :7229): Gdk-CRITICAL **: 11:09:22. 221:gdk_window_get_origin:断言 'GDK_IS_WINDOW (window)' 失败

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:51 24 4
gpt4 key购买 nike

我正在尝试使用 CGTK3 在 Linux Ubuntu 中构建一个应用程序,但我收到关于 gdk_window_get_origin 的错误,我想不通找出我做错了什么。

该应用程序运行并且窗口确实是我的屏幕大小,但是有一个我无法修复的警告:

(Stack:7229): Gdk-CRITICAL **: 11:09:22.221: gdk_window_get_origin: assertion 'GDK_IS_WINDOW (window)' failed Screen max: 1600W X 900H

这是一个示例代码:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
GtkWidget *window;
gint width, height;

gtk_init(&argc, &argv);
/// Creating the Window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/// Get window Size
GdkDisplay *dpy = gtk_widget_get_display( window );
GdkWindow *win = gtk_widget_get_window(window);
GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);

/// Get window Geometry
GdkRectangle geometry;
gdk_monitor_get_geometry(monitor, &geometry);
width = geometry.width;
height = geometry.height;
g_print("Screen max: %dW X %dH\n", width, height);

// Setting the default size of the Window
gtk_window_set_default_size(GTK_WINDOW(window), width, height );

// Destroy the Window
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
}

最佳答案

TL;DR:您正试图在实现 GtkWindow 之前访问 GdkWindow。

来自 gtk_widget_get_window documentation :

Returns the widget’s window if it is realized, NULL otherwise

gtk_widget_realize :

Creates the GDK (windowing system) resources associated with a widget. For example, widget->window will be created when a widget is realized. Normally realization happens implicitly; if you show a widget and all its parent containers, then the widget will be realized and mapped automatically.

您需要在尝试使用 gtk_widget_get_display 访问 GdkWindow 之前调用 gtk_widget_realize 或推迟访问直到实现窗口(在 Gtk 主循环开始后)使用回调到realize信号:

#include <gtk/gtk.h>

void print_geometry(GtkWidget* widget, GdkRectangle* geometry)
{
GdkDisplay *dpy = gtk_widget_get_display(widget);
GdkWindow *win = gtk_widget_get_window(widget);
GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);

gdk_monitor_get_geometry(monitor, geometry);
gint width = geometry->width;
gint height = geometry->height;
g_print("Screen max: %dW X %dH\n", width, height);

gtk_window_set_default_size(GTK_WINDOW(widget), width, height );
}

int main(int argc, char *argv[])
{
GtkWidget *window;

gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

GdkRectangle geometry;
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "realize", G_CALLBACK(print_geometry), &geometry);
gtk_widget_show_all(window);
gtk_main();
}

关于c - (堆栈 :7229): Gdk-CRITICAL **: 11:09:22. 221:gdk_window_get_origin:断言 'GDK_IS_WINDOW (window)' 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54144406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com