c - Gtk+ 加速组回调函数问题



我是 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+ 文档,闭包的签名必须是:

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。但我认为签名中的第二个参数最终会成为窗口,所以我不知道。

最新更新