Working with checkbox (problem)

Description

Hi All,

As in topic, I have a few problems with Checkbox.
First one:
I create a view with checkbox and set its state as LV_STATE_CHECKED. When I start the application, the checkbox is unchecked and its state changes after a while - about 5s (in the code we heve one sleep with 3s before for-loop and then loop from 0 to 2 with 1s sleep).

Second one:
When checkbox is unchecked and I add the state LV_STATE_CHECKED again, the checkbox appeared as white square without tick (I marked the part in the attached code).

Third one:
The properly checked checkbox lose tick when I added state LV_STATE_FOCUSED. I tried to add parent for the checkbox, and change its state to FOCUSED, but the same situation - checkbox is unchecked (only on the screen, in the code checkbox still has checked state).

Am I making something wrong or omitted something? I used the documentation and checked this code in Simulator (similar situation, but checkbox is “FOCUSED” from the beginning).

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

Compiler: g++ (GCC) 12.2.0, gcc (GCC) 12.2.0

What LVGL version are you using?

LVGL 9.1 in Frame Buffer.
But the issue is also in versions 8.2, and from the master branch.

What do you want to achieve?

Properly working Checkbox with CHECKED status. Also, to have possibility to add FOCUSED status.

Code to reproduce

Here is small code where I reproduce problem:

#include "lvgl/lvgl.h"
#include <unistd.h>

int main(int argc, char **argv)
{
    lv_init();
    lv_display_t * disp = lv_linux_fbdev_create();
    lv_linux_fbdev_set_file(disp, "/dev/fb0");

    lv_style_t style_first;
    lv_style_init(&style_first);
    lv_style_set_bg_color(&style_first, lv_color_hex(0xFF00FF));

    lv_style_t style_list_active;
    lv_style_init(&style_list_active);
    lv_style_set_bg_color(&style_list_active, lv_color_hex(0xa5a5c0));
    lv_style_set_border_width(&style_list_active, 3);
    lv_style_set_border_color(&style_list_active, lv_color_hex(0xFFFFFF));

    lv_obj_t* first_view = lv_obj_create(NULL);

    lv_obj_align(first_view, LV_ALIGN_CENTER, 0, 0);  
    lv_obj_set_size(first_view, lv_pct(100), lv_pct(100));
    lv_obj_add_style(first_view, &style_first, 0);

    lv_obj_t* checkbox = lv_checkbox_create(first_view);
    lv_obj_center(checkbox);
    lv_obj_add_style(checkbox, &style_list_active, LV_STATE_FOCUSED);


    lv_checkbox_set_text(checkbox, "(check = true)");
    lv_obj_add_state(checkbox, LV_STATE_CHECKED);

    lv_scr_load(first_view);
    lv_timer_handler();
    sleep(3);
    // **First problem is here**
    for(int i = 0; i < 2; i++) {
        lv_timer_handler();
        sleep(1);
    }

    lv_checkbox_set_text(checkbox, "(check = false)");
    lv_obj_clear_state(checkbox, LV_STATE_CHECKED);
    for(int i = 0; i < 3; i++) {
        lv_timer_handler();
        sleep(1);
    }

    lv_obj_add_state(checkbox, LV_STATE_CHECKED);
    lv_checkbox_set_text(checkbox, "(check = true)");
    // **Second problem is here**
    for(int i = 0; i < 2; i++) {
        lv_timer_handler();
        sleep(1);
    }

    lv_obj_add_state(checkbox, LV_STATE_FOCUSED);
    lv_checkbox_set_text(checkbox, "(check = true) && FOCUSED");
    // **Third problem is here**
    for(int i = 0; i < 3; i++) {
        lv_timer_handler();
        sleep(1);
    }

    lv_obj_clear_state(checkbox, LV_STATE_CHECKED);
    lv_checkbox_set_text(checkbox, "(check = false) && FOCUSED");
    for(int i = 0; i < 3; i++) {
        lv_timer_handler();
        sleep(1);
    }

    lv_obj_add_state(checkbox, LV_STATE_CHECKED);
    lv_checkbox_set_text(checkbox, "(check = true) && FOCUSED");
    for(int i = 0; i < 3; i++) {
        lv_timer_handler();
        sleep(1);
    }


    lv_obj_clear_state(checkbox, LV_STATE_FOCUSED);
    lv_checkbox_set_text(checkbox, "(check = true)");
    for(int i = 0; i < 3; i++) {
        lv_timer_handler();
        sleep(1);
    }
}

Screenshot and/or video

Screenshot just after start the app (checkbox should be checked as it is written in label):

Screenshot from 2024-05-17 12-05-39

You completely missunderstand how lvgl works. You cant sleep for seconds optimaly never. Real use is max delays 5ms or less as lvconf setup.
Next coding states must be event based no pause wait. When your sim dont use loop func minimum for explain is your code after line

lv_scr_load(first_view);
unsigned int looper5=0;
while(1) {
    lv_timer_handler();
    delay(5);
    looper5++;
if( (looper5%200) == 0 )  //or other event
 {
    lv_checkbox_set_text(checkbox, "(check = false)");
    lv_obj_clear_state(checkbox, LV_STATE_CHECKED);
 }
} //end while

plus version 8 and 9 have different syntax , then check your code …

1 Like

Great! Thank you Marian!
Yes, I really misunderstand this, I thought that I need to call lv_timer_handler just once, after each change on the screen. And it worked until I started to use checkboxes…
So in my project, I need, for example, a thread with “inifinite” while loop to call this function.

When I added such thread to the attached code it works very good, so I think it also will help in my project.

LVGL isnt thread safe then use threads result to more complicated code… but work.