GTK - 如何将参数传递给 GtkEventBox
GTK - How to pass an argument to a GtkEventBox
这段代码创建了一个程序,它创建了一个带有标签的 window,如果您单击该标签,该程序将执行作为 g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(on_event_clicked), buffer)
参数传递的缓冲区的 fprintf。以前程序将字符串 缓冲区 放入 "Hello Word" 然后程序应该打印此消息,但程序可能只打印乱码。我哪里错了?
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
void on_event_clicked (GtkWidget* widget, gpointer user_data);
int main (int argc, char **argv) {
GtkWidget *window;
GtkWidget *eventbox;
GtkWidget *label;
char* buffer = malloc(sizeof(char)*10);
strcpy(buffer, "Hello Word[=10=]");
gtk_init (&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
label = gtk_label_new ("Hello Word");
eventbox = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(eventbox), label);
gtk_container_add (GTK_CONTAINER(window), eventbox);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event",
G_CALLBACK(on_event_clicked), buffer);
gtk_main();
return 0;
}
void on_event_clicked (GtkWidget *widget, gpointer user_data) {
char* pn = user_data;
fprintf(stderr, "%s\n", pn);
}
您 on_event_clicked()
的原型是错误的,它与 GTK+ 的预期不符。
应该是:
gboolean user_function (GtkWidget *widget, GdkEvent *event, gpointer user_data);
您需要将缺少的参数添加到您的函数中,并处理对 return 值的要求。记得read the signal documentation认真。
这段代码创建了一个程序,它创建了一个带有标签的 window,如果您单击该标签,该程序将执行作为 g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(on_event_clicked), buffer)
参数传递的缓冲区的 fprintf。以前程序将字符串 缓冲区 放入 "Hello Word" 然后程序应该打印此消息,但程序可能只打印乱码。我哪里错了?
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
void on_event_clicked (GtkWidget* widget, gpointer user_data);
int main (int argc, char **argv) {
GtkWidget *window;
GtkWidget *eventbox;
GtkWidget *label;
char* buffer = malloc(sizeof(char)*10);
strcpy(buffer, "Hello Word[=10=]");
gtk_init (&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
label = gtk_label_new ("Hello Word");
eventbox = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(eventbox), label);
gtk_container_add (GTK_CONTAINER(window), eventbox);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event",
G_CALLBACK(on_event_clicked), buffer);
gtk_main();
return 0;
}
void on_event_clicked (GtkWidget *widget, gpointer user_data) {
char* pn = user_data;
fprintf(stderr, "%s\n", pn);
}
您 on_event_clicked()
的原型是错误的,它与 GTK+ 的预期不符。
应该是:
gboolean user_function (GtkWidget *widget, GdkEvent *event, gpointer user_data);
您需要将缺少的参数添加到您的函数中,并处理对 return 值的要求。记得read the signal documentation认真。