Unable to update object background gradient style from event callback


When setting a gradient style on an object, the gradient does not update when calling lv_obj_set_style_bg_color and lv_obj_set_style_bg_grad_color from an event callback. It seems like once I add an initial gradient by setting the gradient direction, it will not refresh on future changes. I can update background color like this, but not the bg gradient colors. Strangely changing the gradient direction will work! I am not sure if gradients specifically have a different redraw and need to be handled accordingly. This worked on previous LVGL versions, but when updating to 8.3.9 it broke (we are updating so we can enable dithering for gradients)

What MCU/Processor/Board and compiler are you using?

Apple M2 Pro simulator using SDL

What LVGL version are you using?


What do you want to achieve?

When calling lv_obj_set_style_bg_color and lv_obj_set_style_bg_grad_color after a gradient has already been applied, update the object to use the new gradient.

What have you tried so far?

  • Using a separate style applied to the object and updating the property on the style object instead of using lv_obj_set_style methods
  • Calling lv_obj_invalidate(background_block) after setting a new gradient color
  • Calling 'lv_obj_invalidate(lv_scr_act())
  • Calling lv_obj_refresh_style(background_block, LV_PART_MAIN, LV_STYLE_PROP_ANY)

Code to reproduce

Example code with a callback updating the background gradient color.

lv_obj_t* background_block;
lv_obj_t* button;

void callback() {
  lv_obj_set_style_bg_color(background_block, lv_color_hex(0x444444), LV_PART_MAIN);
  lv_obj_set_style_bg_grad_color(background_block, lv_color_hex(0x555555), LV_PART_MAIN);

void main() {
  background_block = lv_obj_create(lv_scr_act());
  lv_obj_align(background_block, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(background_block, lv_pct(100), lv_pct(100));

  lv_obj_set_style_bg_grad_dir(background_block, LV_GRAD_DIR_HOR, LV_PART_MAIN);
  lv_obj_set_style_bg_color(background_block, lv_color_hex(0x111111), LV_PART_MAIN);
  lv_obj_set_style_bg_grad_color(background_block, lv_color_hex(0x222222), LV_PART_MAIN);

  button = lv_btn_create(lv_scr_act());
  lv_obj_align(button, LV_ALIGN_CENTER, 0, 0);
  lv_obj_add_event_cb(button, callback, LV_EVENT_CLICKED, NULL);