Change LVGL label text in code

This should be real simple but I cannot find it.
I am using LVGL v9.1.0.. On one of my screens is a label on a button .
once that button is clicked it calls a function in platform IO (working great).
If the called function is running, I want to change the button label to .

Points to note: the button already exists as it calls a function.
I have tried to read as much LVGL but it reads like mud.
Would like to get the object by some method and then I can change text.

Any ideas??

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum

There are several possible approaches, depending on how you've created your widget and programmed your callbacks.

If the following examples don't solve your problem, please read "How to get the best out of this forum" and post any helpful information (especially any relevant code enclosed in <code /> tags).

Tip:

  • Use lv_obj_get_child() in the callback function to get the text label.
  • Or, make the text label object created by lv_label_create() as a global variable.

And your post...

Alternatively, in lvgl 9.3 and later you may be able to use lv_obj_set_id() and lv_obj_find_by_id().

Also you may be able to use lv_obj_find_by_name() in lvgl 9.3 and later.

(LV_USE_OBJ_NAME must be set to 1 in lv_conf.h)

I am trying to migrate to LVGL 9.3 where there are more features as you described, However getting several compile errors:
lib/ui/eez-flow.cpp: In function 'int32_t eez::flow::anim_callback_get_opacity(lv_anim_t*)':
lib/ui/eez-flow.cpp:3837:113: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]

lib/ui/eez-flow.cpp: In function 'void eez::flow::executeLVGLComponent(FlowState*, unsigned int)':
lib/ui/eez-flow.cpp:4019:49: error: too many arguments to function 'void lv_dropdown_set_selected(lv_obj_t*, uint32_t)'
4019 | lv_dropdown_set_selected(target, intValue, LV_ANIM_OFF);
| ^~~~~~~

lib/ui/eez-flow.cpp: In function 'int32_t eez::flow::anim_callback_get_opacity(lv_anim_t*)':
lib/ui/eez-flow.cpp:3837:113: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]

lib/ui/eez-flow.cpp: In function 'void eez::flow::objGetStyleOpa(FlowState*, unsigned int, const eez::ListOfAssetsPtreez::Property&, uint32_t)':
lib/ui/eez-flow.cpp:4408:54: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]
4408 | int32_t opa = (int32_t)lv_obj_get_style_opa(obj, 0);

Any thoughts?

Post your error log in a code block.

lib/ui/eez-flow.cpp: In function 'int32_t eez::flow::anim_callback_get_opacity(lv_anim_t*)':
lib/ui/eez-flow.cpp:3837:113: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]
 622 | static inline lv_opa_t lv_obj_get_style_opa(const lv_obj_t * obj, lv_part_t part)
      |                                                                   ~~~~~~~~~~^~~~
lib/ui/eez-flow.cpp: In function 'void eez::flow::executeLVGLComponent(FlowState*, unsigned int)':
Compiling .pio\build\esp32dev\lib216\Network\NetworkUdp.cpp.o
lib/ui/eez-flow.cpp:4019:49: error: too many arguments to function 'void lv_dropdown_set_selected(lv_obj_t*, uint32_t)'
 4019 |                         lv_dropdown_set_selected(target, intValue, LV_ANIM_OFF);

Compiling .pio\build\esp32dev\lib995\WiFi\AP.cpp.o 112 | void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt); | ^~~~~~~~~~~~~~~~~~~~~~~~ lib/ui/eez-flow.cpp: In function 'void eez::flow::objGetStyleOpa(FlowState*, unsigned int, const eez::ListOfAssetsPtreez::Property&, uint32_t)': lib/ui/eez-flow.cpp:4408:54: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive] 4408 | int32_t opa = (int32_t)lv_obj_get_style_opa(obj, 0); | ^ | | | int

lib/lvgl/src/core/lv_obj_style_gen.h:622:77: note:   initializing argument 2 of 'lv_opa_t lv_obj_get_style_opa(const lv_obj_t*, lv_part_t)'
  622 | static inline lv_opa_t lv_obj_get_style_opa(const lv_obj_t * obj, lv_part_t part)
      |                                                                   ~~~~~~~~~~^~~~
*** [.pio\build\esp32dev\lib82a\ui\eez-flow.cpp.o] Error 1

I have very little experience with EEZ Studio and am completely unfamiliar with EEZ Flow.

However, looking at the error messages, these errors require a fix in EEZ Studio, so I think you should actually request a fix in Issues.


1. Errors related to lv_part_t:

lib/ui/eez-flow.cpp: In function 'int32_t eez::flow::anim_callback_get_opacity(lv_anim_t*)':
lib/ui/eez-flow.cpp:3837:113: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]
lib/ui/eez-flow.cpp:4408:54: error: invalid conversion from 'int' to 'lv_part_t' [-fpermissive]
4408 | int32_t opa = (int32_t)lv_obj_get_style_opa(obj, 0);

There are no practical issues with 32-bit MCUs, but the Arduino IDE does not seem to allow them.

You may be able to downgrade these to a warning level by adding -fpermissive to your platform.txt.

Alternatively, the original code:

static int32_t anim_callback_get_opacity(lv_anim_t * a) { return lv_obj_get_style_opa((lv_obj_t *)a->user_data, 0); }

should be modifed:

static int32_t anim_callback_get_opacity(lv_anim_t * a) { return lv_obj_get_style_opa((lv_obj_t *)a->user_data, LV_PART_MAIN); }

or

static int32_t anim_callback_get_opacity(lv_anim_t * a) { return lv_obj_get_style_opa((lv_obj_t *)a->user_data, (lv_part_t)0; }

2. Error related to lv_dropdown_set_selected()

lib/ui/eez-flow.cpp:4019:49: error: too many arguments to function 'void lv_dropdown_set_selected(lv_obj_t*, uint32_t)'
4019 | lv_dropdown_set_selected(target, intValue, LV_ANIM_OFF);
| ^~~~~~~

Since the API of lv_dropdown_set_selected() has only 2 arguments in LVGL 9.3 and up:

void lv_dropdown_set_selected(lv_obj_t *obj, uint32_t sel_opt)

So the original code:

#if LVGL_VERSION_MAJOR >= 9 && LVGL_VERSION_MINOR >= 3
	lv_dropdown_set_selected(target, intValue, LV_ANIM_OFF);
#else 
	lv_dropdown_set_selected(target, intValue);
#endif

should be:

#if LVGL_VERSION_MAJOR >= 9 && LVGL_VERSION_MINOR >= 3
	lv_dropdown_set_selected(target, intValue);
#else 
	lv_dropdown_set_selected(target, intValue);
#endif

Note that the LVGL API specification changes frequently.

I was finally able to solve this, took a bit but here it is:
USING lvgl v9.3

in the screens.c file I had to add

objects.lblbut_scan = obj;
lv_obj_set_pos(obj, -1, 0);
lv_obj_set_name(obj, “scan_button_label”); << added to set the name

in my main.ccp code had to get reference to the object
Getting the button required
lv_obj_t * objbutton = lv_obj_get_child_by_name(lv_screen_active(), "scan_button");
lv_obj_t * objbuttontxt = lv_obj_get_child_by_name(objbutton, "scan_button_label");

by doing that I was able to
lv_label_set_text(objbuttontxt, “scanning…”);
more code was required but there are 2 steps needed