Clean method to swap a component's child widget

What do you want to achieve?

We have ui_comp_get_child(), I’d like something like ui_comp_set_child().

Do you see alternative options and workaround to achieve it?

I don’t. I tried using lb_obj_t ** children = lv_obj_get_user_data(comp), but the children are not attached to the component instance, rather they are attached to the LV_EVENT_GET_COMP_CHILD’s user-data. In either case this is approaching the problem at the wrong layer.

Mention some use cases

For UI popups, we have a complex component that can be configured different ways to produce what we want. I need to add a child widget(QR code) that is not available in SquareLine studio. I’d like to create a placeholder child widget that I can swap out with a QR widget when the component is configured at runtime.

Maybe you cen use set parent …

Alas, I cannot here. lv_obj_set_parent() will only update the new child’s parent to the component, but will leave the component’s child I’d like to replace.

In real world swap/replace dont exist. Is add new remove old…

Agreed. Sorry, I don’t think I explained it clearly enough.

Each child of the SquareLine component has two relationships to the parent component instance:

  1. The standard LVGL parent->children relationship.
  2. The SquareLine LV_EVENT_GET_COMP_CHILD user-data relationship.

After the swap is done, I’d like ui_comp_get_child() to continue working, but lv_obj_set_parent() won’t handle that.

I would put a widget (e.g. an image) in place of the QR code in the SLS project, give it a Custom Variable Alias, and then using this, I would replace it with the QR code before display at runtime. In the SLS export code, in “your component name”.c, you can look up the original widget code.

1 Like