Lv_obj_add_event_cb last parameter

Could someone better explain the purpose of the 4th argument in lv_obj_add_event_cb and possibly provide an example of it being utilized?

All the instances where I utilize the function I keep the 4th paramter as NULL and I would like to understand it better.


It’s a pointer to any user data you would like to pass to the event callback, you can use it to point to a variable/struct you want to have access in the event callback, the variable/struct must be static or global.

In v9 lv_example_checkbox_2 the 4th parameter is used to pass a pointer to a global variable. In the event handler active_id is a pointer to active_index_1 when cont1 is clicked.

static void radio_event_handler(lv_event_t * e)
    uint32_t * active_id = lv_event_get_user_data(e);
    lv_obj_t * cont = lv_event_get_current_target(e);
    lv_obj_t * act_cb = lv_event_get_target(e);
    lv_obj_t * old_cb = lv_obj_get_child(cont, *active_id);

    /*Do nothing if the container was clicked*/
    if(act_cb == cont) return;

    lv_obj_remove_state(old_cb, LV_STATE_CHECKED);   /*Uncheck the previous radio button*/
    lv_obj_add_state(act_cb, LV_STATE_CHECKED);     /*Check the current radio button*/

    *active_id = lv_obj_get_index(act_cb);

    LV_LOG_USER("Selected radio buttons: %d, %d", (int)active_index_1, (int)active_index_2);

/* ... */
lv_obj_add_event_cb(cont1, radio_event_handler, LV_EVENT_CLICKED, &active_index_1);