我无法管理上下文菜单,这是我的代码
#include <gtk/gtk.h>
int on_Form1_button_press_event(GtkWidget *widget, GdkEvent *event, gpointer *user_data) {
g_print ("Button pressed.\n");
const gint RIGHT_CLICK = 3;
if (event->type == GDK_BUTTON_PRESS) {
GdkEventButton *bevent = (GdkEventButton *) event;
if (bevent->button == RIGHT_CLICK) {
g_print ("Right Clicked.\n");
gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
}
return TRUE;
}
return FALSE;
}
int main(int argc, char *argv[]) {
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *menu1;
GtkWidget *item1;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "FormPopup.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "Form1"));
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "menu1"));
item1 = GTK_WIDGET(gtk_builder_get_object(builder, "item1"));
g_signal_connect_swapped (G_OBJECT(window), "button-press-event", G_CALLBACK(on_Form1_button_press_event), menu1);
g_signal_connect(G_OBJECT(item1), "activate", G_CALLBACK(gtk_main_quit), NULL);
g_object_unref(builder);
gtk_widget_show_all(window);
gtk_main();
return 0; }
glade文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="Form1">
<property name="can_focus">False</property>
<signal name="button-press-event" handler="on_Form1_button_press_event" swapped="no"/>
<child>
<placeholder/>
</child>
<child type="titlebar">
<placeholder/>
</child>
</object>
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="item1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">quit</property>
<property name="use_underline">True</property>
<signal name="activate" handler="on_item1_activate" swapped="no"/>
</object>
</child>
</object>
</interface>
错误消息:
GLib-GObject-WARNING **: 17:58:37.126: invalid unclassed pointer in cast to 'GtkMenu'
Gtk-CRITICAL **: 17:58:37.127: gtk_menu_popup: assertion 'GTK_IS_MENU (menu)' failed
谢谢