How to pass parameter to an event_handler

Hi there,

I would like to pass an extra parameter to an event handler. As of now, event handlers have lv_obj_t and lv_event_t as parameter type. When the event is associated to something using lv_obj_set_event_cb(lv_obg_t, func). I would like my func to pass more than the two previously mentioned parameters. Is that possible ?

Thanks in advance,

It depends on whether you are trying to store the extra parameters with the event handler, or send extra data with a custom event.

In v7, you cannot do the former, however, since you can only have one event handler registered per object, you can just use the object’s user data field to store the extra information.

On the other hand, if you are sending custom data with the event, you can retrieve it using lv_event_get_data.

In v8, the event signature has changed and you can now store user data for each event handler.

The docs are not complete yet for v8 but you can read the preliminary page on events here:

What state is v8? Is it unstable/experimental or something?

v8 should (finally) be pretty much stable. (It was supposed to be stable in February but a number of issues were discovered which required more API changes.)

However, the documentation is not complete yet, so you will have to do quite a bit of reading through the code to figure out how the new APIs work. If you are looking for stability, I would suggest sticking with v7 for now, as the documentation there is largely complete and there aren’t going to be any more changes to it besides bugfixes.

1 Like

Thanks for the overview, @embeddedt

Usually, I’d go with stable.

But I am about to add some functionality to v7, so I wonder whether this might already be addressed in v8.

BTW: Where is the right place to discuss such topics? “Feature request” or “General discussion”?


Thanks for the reply. I still have a problem though. Assume I have a custom_type * object, so a pointer to a custom type (structure in my case) called object. I want to pass object by reference to the event handler function but it looks like I can’t. If I check the address of what I get from lv_event_get_data() it is different from the object pointer. Is there a way to solve this ?

If you just pass the pointer to lv_event_send, not a pointer to the pointer, that should work. If it still doesn’t, please send a small code snippet and I’ll take a look.

General discussion is probably the best category for discussions about v8’s stability. However I really won’t mind if you post in “Feature request” instead. :wink:

I have this global variable called:

tavola_impostazioni tav1;

I then have a function called:

  • void tavola_impostazioni_create(tavola_impostazioni *object, lv_obj_t * parent);

and I pass tav1 in the following way: tavola_impostazioni_create(&tav1,parent);

Inside tavola_impostazioni_create() I then call the following function:

generate_empty_table(object, parent);

Which is defined as follows:

  • void generate_empty_table(tavola_impostazioni *object, lv_obj_t * parent);

Then, inside generate_empty_table() I want to pass the object to an event handler called


Problem is that when I try to get back my original object inside select_event_handler it seems like the object pointer is not pointing anymore to &tav1. The *object in the previous functions ( tavola_impostazioni_create and generate_empty_table ) is pointing towards the right address of tav1. Once the object is given to the select_event_handler with lv_event_sent the object pointer inside select_event_handler is not pointing to the right address anymore.

Let’s say I get back my object pointer in the following way inside select_event_handler:

  • tavola_impostazioni * get = lv_event_get_data();

Look at the address changes:

tav1 is located at 0x55a83a5ea9c0
tavola_impostazioni_create object pointer at 0x7ffe62149ee8
tavola_impostazioni_create object points to 0x55a83a5ea9c0
generate empty table object pointer at 0x7ffe62149eb8
generate empty table objects points to 0x55a83a5ea9c0
inside init_bat
inside init_rad
inside init_reg
inside init_reg
select_event_handler object pointer from lv_event_get_data at address 0x7ffe62149c80
select_event_handler object pointer from lv_event_get_data points to 0x7ffe62149d6c

I hope that was clear enough for you to understand.

Ok, I solved the issue but not using what you suggested. Instead I used lv_obj_set_user_data() to set the user data to the object and then lv_obj_get_user_data(object) to get the user data back. So the previous way it creates a new instance of the user data being assigned to the object while this one does not. More specifically, I think is the function used to get the object back. To sum up:

lv_event_get_data() -> creates a new instance.
lv_obj_get_user_data(object) -> returns you the original pointer.

I hope it makes sense to you as well.