当前位置: 技术问答>linux和unix
一个gtk的简单问题,解决100分送出
来源: 互联网 发布时间:2016-04-19
本文导语: 代码如下: #include #include GtkWidget *button1; gboolean button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data) { g_message("button_press_event"); return TRUE; } gboolean keybord_cb(GtkWidget *widget, GdkEventKey *key, gpointer data) {...
代码如下:
#include
#include
GtkWidget *button1;
gboolean button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
g_message("button_press_event");
return TRUE;
}
gboolean keybord_cb(GtkWidget *widget, GdkEventKey *key, gpointer data)
{
g_message("keybord_cb");
g_signal_emit_by_name(G_OBJECT(button1), "button-press-event");
return TRUE;
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *entry;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "key-press-event", G_CALLBACK(keybord_cb), NULL);
vbox = gtk_vbox_new(FALSE, 10);
gtk_container_add(GTK_CONTAINER(window), vbox);
button = gtk_button_new_with_label("button");
gtk_box_pack_start(GTK_BOX(vbox), button, 1, 1, 0);
entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(vbox), entry, 1, 1, 0);
g_signal_connect(button, "button-press-event", G_CALLBACK(button_press_event), GINT_TO_POINTER(1));
button1 = button;
gtk_widget_show_all(window);
gtk_main();
return 0;
}
运行后界面可以出来
但一旦按了一个键盘上的键
就出现段错误
问题基本可以确定是出现在g_signal_emit_by_name(G_OBJECT(button1), "button-press-event");这里
求解决
急!
#include
#include
GtkWidget *button1;
gboolean button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
g_message("button_press_event");
return TRUE;
}
gboolean keybord_cb(GtkWidget *widget, GdkEventKey *key, gpointer data)
{
g_message("keybord_cb");
g_signal_emit_by_name(G_OBJECT(button1), "button-press-event");
return TRUE;
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *entry;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "key-press-event", G_CALLBACK(keybord_cb), NULL);
vbox = gtk_vbox_new(FALSE, 10);
gtk_container_add(GTK_CONTAINER(window), vbox);
button = gtk_button_new_with_label("button");
gtk_box_pack_start(GTK_BOX(vbox), button, 1, 1, 0);
entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(vbox), entry, 1, 1, 0);
g_signal_connect(button, "button-press-event", G_CALLBACK(button_press_event), GINT_TO_POINTER(1));
button1 = button;
gtk_widget_show_all(window);
gtk_main();
return 0;
}
运行后界面可以出来
但一旦按了一个键盘上的键
就出现段错误
问题基本可以确定是出现在g_signal_emit_by_name(G_OBJECT(button1), "button-press-event");这里
求解决
急!
|
第三个参数NULL是传给g_signal_emit_by_name
走的是一个默认调用。
走的是一个默认调用。