如何在循环中发送带有gtk信号的user_data [英] How to send user_data with gtk signals in loop

查看:49
本文介绍了如何在循环中发送带有gtk信号的user_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主线程的核心类中注册了信号"make"和"send".我在同一线程中从用户类调用 call_make(). call_make()将做一些工作,并从工作线程中获取一些数据,返回后,它将发出"make"信号.

I have signals "make" and "send" registered in core class in main thread. I call call_make() from user class in same thread. call_make() will do some work and fetch some data from worker thread and after return it will emit signal "make".

这是用户类别的代码

struct my_struct{
   int cur_make_id;
   int cur_send_id;    //there may be many id's if multiple signals
}

void on_send_cb(core, gpointer data){
   my_struct *user_data = (my_struct *) data;
   printf("cure_make_id %d", user_data->cur_make_id);   \\ this prints "4" everytime  
   printf("cure_send_id %d", user_data->cur_send_id);   \\ this prints "2" everytime  
}

void on_make_cb(core, gpointer data){
   my_struct *user_data = (my_struct *) data;
   printf("cure_make_id %d", user_data->cur_make_id);   \\ this prints "4" everytime  
   for(int index=0;index<3;index++){
       call_send();
       user_data->cur_send_id = index;
       g_signal_connect(core, "send", G_CALLBACK(on_send_cb), user_data );
   }     
}

void function(){
   my_struct *user_data = g_malloc0(sizeof(my_struct));
   for(int index=0;index<5;index++){
       call_make();
       user_data->cur_make_id = index;
       g_signal_connect(core, "make", G_CALLBACK(on_make_cb), user_data );
   }
}

对于所有信号我得到错误的cur_make_id和cur_send_id(获取for循环的最后一个索引)

I get wrong cur_make_id and cur_send_id for all signals (getting last index of for loop)

我知道signal_emit并延迟连接,并且for循环向前移动.我想知道如何共享此user_data进行回调,以便知道该回调适用于哪个索引?

I know signal_emit and connect delayed and for loop moved forward. I want to know how can I share this user_data to callback so that I can know that this callback is for which index?

(为简单起见,我制作了这些 call_make() api,但我无法更改api的参数)

(for simplicity I made these call_make() api's, I can't change arguments of api's)

推荐答案

假定您仅在谈论在连接回调期间提供的 user_data ,那么您的内存分配有误:

Assuming you are only talking about the user_data provided during connecting the callback, you have wrong memory allocation:

void function(){
   my_struct *user_data = g_malloc0(sizeof(my_struct));
   for(int index=0;index<5;index++){
       call_make();
       user_data->cur_make_id = index;
       g_signal_connect(core, "make", G_CALLBACK(on_make_cb), user_data );
   }
}

在此函数中,只有1个内存块传递给所有连接的回调函数.每当调用回调时,它们都会看到最后写的内容.在您的情况下,这将是 index = 4 .

In this function you have only 1 single block of memory that is passed to all connected callback functions. Whenever the callbacks are called, they will see whatever was written there last. In your case this will be index=4.

要为不同的处理程序提供不同的数据,实际上您需要分配不同的数据:

To provide different data for different handlers, you actually need to allocate different data:

void function(){
   for(int index=0;index<5;index++){
       my_struct *user_data = g_malloc0(sizeof(my_struct));
       call_make();
       user_data->cur_make_id = index;
       g_signal_connect(core, "make", G_CALLBACK(on_make_cb), user_data);
   }
}

如果在触发信号的函数中提供数据时也遇到问题,则需要显示更多代码.

If you also have problems providing data from the function triggering the signal, you need to show more code.

这篇关于如何在循环中发送带有gtk信号的user_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆