当前位置: 技术问答>linux和unix
更怪异的GtkLabel对象传递问题
来源: 互联网 发布时间:2016-05-24
本文导语: 再发一个更TMD怪异的,我一共有三条信号的回调函数传GtkLabel对象的地址,但是,有一条出错。点上去后直接挂掉,典型的段错误。DEBUG去看,值没传过去。代码如下: #include #include #include #include void radio_acti...
再发一个更TMD怪异的,我一共有三条信号的回调函数传GtkLabel对象的地址,但是,有一条出错。点上去后直接挂掉,典型的段错误。DEBUG去看,值没传过去。代码如下:
#include
#include
#include
#include
void radio_activate(GtkRadioMenuItem *menuitem,
GtkLabel *label) {
char citem[100];
sprintf(citem, "被选中的单选菜单项为:%s",
gtk_label_get_label(GTK_LABEL(GTK_BIN(menuitem) -> child)));
gtk_label_set_label(label, citem);
}
void label_const(GtkEventBox *eventbox,
GtkLabel *label)
{
static int i = 0;
static char citem[100];
sprintf(citem, "标签单击的次数:%d", ++i);
gtk_label_set_label(label, citem); //!!!!! 问题出这里
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "单选菜单演示");
gtk_widget_show(window);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *vbox;
vbox = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (window), vbox);
GtkWidget *menubar;
menubar = gtk_menu_bar_new ();
gtk_widget_show (menubar);
gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
GtkWidget *rootitem;
rootitem = gtk_menu_item_new_with_label ("菜单");
gtk_widget_show (rootitem);
gtk_container_add (GTK_CONTAINER (menubar), rootitem);
GtkWidget *rootitem_menu;
rootitem_menu = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (rootitem), rootitem_menu);
GSList *group = NULL;
GtkWidget *son1;
son1 = gtk_radio_menu_item_new_with_label (group, "单选菜单项1");
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (son1), TRUE);
group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (son1));
gtk_widget_show (son1);
gtk_container_add (GTK_CONTAINER (rootitem_menu), son1);
GtkWidget * son2;
son2 = gtk_radio_menu_item_new_with_label(group, "单选菜单项2");
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(son2));
gtk_widget_show(son2);
gtk_container_add(GTK_CONTAINER(rootitem_menu), son2);
GtkWidget *eventbox;
eventbox = gtk_event_box_new();
gtk_widget_show(eventbox);
gtk_box_pack_start (GTK_BOX (vbox), eventbox, FALSE, FALSE, 0);
GtkWidget *label;
label = gtk_label_new ("请修改单选菜单项状态");
gtk_widget_show (label);
gtk_misc_set_padding (GTK_MISC (label), 0, 120);
gtk_container_add(GTK_CONTAINER(eventbox), label);
g_signal_connect((gpointer) son1, "activate", G_CALLBACK(radio_activate),
(gpointer) label);
g_signal_connect((gpointer) son2, "activate", G_CALLBACK(radio_activate),
(gpointer) label);
g_signal_connect((gpointer) eventbox, "button_press_event", G_CALLBACK(
label_const), (gpointer) label); // !!!!!!! 就这条传过去出问题
gtk_main();
return 0;
}
|
button_press_event
事件的回调函数也需要多一个参数
void label_const(GtkEventBox *eventbox,
GtkLabel *label)
改成
void label_const(GtkEventBox *eventbox,
GdkEvent *event,
GtkLabel *label)
应该就可以了
事件的回调函数也需要多一个参数
void label_const(GtkEventBox *eventbox,
GtkLabel *label)
改成
void label_const(GtkEventBox *eventbox,
GdkEvent *event,
GtkLabel *label)
应该就可以了