David
November 17, 2021, 8:37am
#1
Description
Is it possible to create several objects such as buttons dynamically via malloc?
What MCU/Processor/Board and compiler are you using?
Arduino Due (Cortex M3), Arduino IDE
What LVGL version are you using?
7.11
What have you tried so far?
Something like this:
static void dyn_create(lv_obj_t * parent)
{
uint16_t size_dummy_label =30;
lv_obj_t * dummy_text_label = lv_textarea_create(parent, NULL);
lv_obj_t * text_label;
text_label =(dummy_text_label *)malloc(size_dummy_label * sizeof(dummy_text_label));
for(int i=0; i<=size_dummy_label ; i++)
{
text_label[i]=lv_textarea_create(parent, NULL);
lv_obj_set_size(dummy_text_label[i], 20, 10);
lv_obj_align(dummy_text_label[i], NULL, LV_ALIGN_CENTER, -145+, -70+(i*10));
}
}
David:
lv_textarea_create
As far as I understand every create-method uses lv_mem_alloc for newly created object, so you couldn’t use additional alloc for it. To free allocated memory you need to use lv_obj_del
1 Like
David
November 17, 2021, 10:50am
#3
Hallo glory-man,
thanks for your answer.
I’ve now found a way to creat an array with objects i need dynamicly.
typedef struct
{
lv_obj_t * dummy_text_label;
}my_struct;
my_struct * MyStruct = (my_struct *)malloc(sizeof(my_struct)*5);
MyStruct[3].dummy_text_label = lv_textarea_create(parent, NULL);
lv_textarea_add_text(MyStruct[3].dummy_text_label, "Hallo");```
It works well.
David:
lv_obj_t * dummy_text_label;
}my_struct;
my_struct * MyStruct = (my_struct *)malloc(sizeof(my_struct)*5);
MyStruct[3].dummy_text_label = lv_textarea_create(parent, NULL);
lv_textarea_add_text(MyStruct[3].dummy_text_label, "Hallo");
I think such code also should work
lv_obj_t * dummy_text_label[5];
dummy_text_label[3] = lv_textarea_create(parent, NULL);
lv_textarea_add_text(dummy_text_label[3], "Hallo");
David
November 17, 2021, 12:14pm
#5
Yes it should too, but not dynamicly while the program is running. And that’s the point.
if you call this function 5 times
void dyn_create(lv_obj_t * parent)
{
lv_obj_t * dummy_text_label[5];
for(int i = 0; i < 5; i++)
{
dummy_text_label[i] = lv_textarea_create(parent, NULL);
lv_textarea_add_text(dummy_text_label[i], "Hallo");
}
}
your program will creates 25 textarea objects.
Also you can use such programm with a similar result
void dyn_create(lv_obj_t * parent)
{
for(int i = 0; i < 5; i++)
{
lv_obj_t * dummy_text_label = lv_textarea_create(parent, NULL);
lv_textarea_add_text(dummy_text_label, "Hallo");
lv_obj_align(dummy_text_label, NULL, LV_ALIGN_CENTER, -145+i, -70+(i*10));
}
}