gpt4 book ai didi

c - Gtk+ accel组回调函数问题

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

我是 GTK+ 的新手,我不明白我的回调函数有什么问题。你能帮帮我吗?

我想改变我的盒子方向,但我的盒子指针在我的回调函数中是错误的。

我的代码:

#include <gtk/gtk.h>
#include <stdio.h>

void flip_buttons(GtkWidget *window, gpointer user_data) {
gtk_orientable_set_orientation(
GTK_BOX(user_data),
GTK_ORIENTATION_VERTICAL);
}

int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *button;
GtkWidget *button2;

gtk_init (&argc,&argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

button = gtk_button_new_with_label ("Btn A");
button2 = gtk_button_new_with_label ("Btn B");

gtk_container_add (GTK_CONTAINER(window), box);

gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (box), button2, TRUE, TRUE, 0);

gtk_widget_show_all (window);

g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

GClosure* closure_flip_buttons =
g_cclosure_new(G_CALLBACK(flip_buttons), box, 0);

// Set up the accelerator group.
GtkAccelGroup* accel_group = gtk_accel_group_new();

gtk_accel_group_connect(accel_group,
GDK_KEY_F,
GDK_CONTROL_MASK,
0,
closure_flip_buttons);

gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

gtk_main();

return 0;
}

编译时出错:

gtk_accel_error.c: In function ‘flip_buttons’:
/usr/include/glib-2.0/gobject/gtype.h:2277:6: warning: passing argument 1 of ‘gtk_orientable_set_orientation’ from incompatible pointer type [-Wincompatible-pointer-types]
((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
/usr/include/glib-2.0/gobject/gtype.h:482:66: note: in expansion of macro ‘_G_TYPE_CIC’
#define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))
^~~~~~~~~~~
/usr/include/gtk-3.0/gtk/gtkbox.h:40:34: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
#define GTK_BOX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_BOX, GtkBox))
^~~~~~~~~~~~~~~~~~~~~~~~~~

最佳答案

根据 GTK+ documentation ,您的闭包签名必须是:

gboolean
(*GtkAccelGroupActivate) (GtkAccelGroup *accel_group,
GObject *acceleratable,
guint keyval,
GdkModifierType modifier);

我认为这意味着您的函数需要看起来像这样:

gboolean flip_buttons(GtkAccelGroup *accel_group,
GObject *acceleratable,
guint keyvalue,
GdkModifierType modifier,
gpointer user_data)
{
gtk_orientable_set_orientation(
GTK_ORIENTABLE(user_data),
GTK_ORIENTATION_VERTICAL);
return TRUE;
}

但是,您可能需要尝试一下。该警告表明您的 user_data——调用中的第二个参数——是一个 GtkAccelGroup。但我认为签名中的第二个参数最终会成为窗口,所以我不知道。

关于c - Gtk+ accel组回调函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681618/

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