Style is being inherited from immediate parent only

Description

It looks as though style attributes are being inherited from the immediate parent only and not from the parent’s parent.

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

Using the simulator.

What do you experience?

I have a style where I set the font color to red.

Then I create the following:

  • an outer container
  • an inner container which is a child of the outer container
  • a label which is a child of the inner container

If I add the style to the inner container, my label displays red text as I expect.

If I add the style to the outer container, my label displays default black text.

What do you expect?

I was expecting that style inheritance would cascade down multiple layers. But it seems as if inheritance is only happening from the immediate parent.

Code to reproduce

	lv_style_init(&s1);
	lv_style_set_bg_color(&s1, LV_STATE_DEFAULT, LV_COLOR_WHITE);
	lv_style_set_text_color(&s1, LV_STATE_DEFAULT, LV_COLOR_RED);

	outerContainer = lv_cont_create(lv_scr_act(), NULL);
	innerContainer = lv_cont_create(outerContainer, NULL);

#ifdef USE_NESTED_CONTAINERS
	lv_obj_add_style(outerContainer, LV_OBJ_PART_MAIN, &s1); // << label does not pick up red text color
#else
	lv_obj_add_style(innerContainer, LV_OBJ_PART_MAIN, &s1); // << label will pick up red text color
#endif

	lv_cont_set_fit4(innerContainer, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT);
	lv_cont_set_layout(innerContainer, LV_LAYOUT_OFF);

	lbl = lv_label_create(innerContainer, NULL);
	lv_label_set_text(lbl, "Hello World");

Hi,

it happens because, in the default theme, font color is set on the black on the basic object.
So if you set the font color to red on outerContainer, innerContainer still has text_color=balck by default, so outerContainer won’t be considered.

Try this zo remove the default styles:

  lv_obj_t * outerContainer = lv_cont_create(lv_scr_act(), NULL);
  lv_obj_clean_style_list(outerContainer, LV_OBJ_PART_MAIN);
  lv_obj_t * innerContainer = lv_cont_create(outerContainer, NULL);
  lv_obj_clean_style_list(innerContainer, LV_OBJ_PART_MAIN);
1 Like

Thank you. That explains what I’m seeing. Cleaning the style list now allows the font color to cascade.

1 Like

How to clean only one default property(text_color)?