gpt4 book ai didi

c - 为什么我的 GTK+ 信号回调中出现段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:30 25 4
gpt4 key购买 nike

我正在尝试测量 GTK 标签的大小:

#include <gtk/gtk.h>

static void map_event(GtkWidget *window, gpointer lab) {
g_print( "In the callback..\n" );
GtkWidget *label = GTK_WIDGET(lab);
g_print( "Everything is ok..\n" );
}

static void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window1");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 280);
GtkWidget *grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
GtkWidget *label = gtk_label_new("Hello world!");
gtk_grid_attach(GTK_GRID (grid), label, 0,0,1,1);
g_signal_connect (window, "map-event", G_CALLBACK(map_event), label);
gtk_widget_show_all (window);
}

int main (int argc, char **argv) {
GtkApplication *app = gtk_application_new (
"org.gtk.example", G_APPLICATION_FLAGS_NONE );
g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref (app);

return status;
}

这给出了输出:

In the callback..
Segmentation fault (core dumped)

如果我注释掉这一行:

GtkWidget *label = GTK_WIDGET(lab);

没有段错误,标 checkout 现,输出为:

In the callback..
Everything is ok..

我在这里错过了什么?

最佳答案

map-event具有以下签名,因此您缺少 GdkEvent* 参数:

gboolean
user_function (GtkWidget *widget,
GdkEvent *event,
gpointer user_data)

不幸的是,GTK+是用C写的,所以它缺少类型安全的回调函数,所以很容易犯这样的错误。

关于c - 为什么我的 GTK+ 信号回调中出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200255/

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