How to change the background color after the table was create?(V8)

Important: unclear posts may not receive useful answers.

how to change the background color after the table was create?(V8)

Before posting


I need change the background color of the table after it was create. I have use ‘lv_obj_draw_part_dsc_t’ in the callback function to do this, but it seems like it creat another layer on the top and the label have been covered. The scene is when I create a table with fixed color, when I click one of cell in this table, the background of the cell is changed and the label still on it. At this moment I do not know how to do it, I would appreciate if anyone could give me some advice!

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

What LVGL version are you using?


What do you want to achieve?

click the cell of table the background color changed and the label still on it.

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

#include "../../lv_examples.h"

#define ITEM_CNT 200

static void draw_event_cb(lv_event_t * e)
    lv_obj_t * obj = lv_event_get_target(e);
    lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
    /*If the cells are drawn...*/
    if(dsc->part == LV_PART_ITEMS) {
        uint32_t irow = dsc->id /  lv_table_get_col_cnt(obj);
        uint32_t icol = dsc->id - irow * lv_table_get_col_cnt(obj);

        bool chk = lv_table_has_cell_ctrl(obj, irow, icol, LV_TABLE_CELL_CTRL_CUSTOM_1);
        //bool chk = lv_table_has_cell_ctrl(obj, dsc->id, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
            dsc->rect_dsc->bg_color = lv_color_hex(0x6495ED);
            //dsc->rect_dsc->bg_opa = 0;
            dsc->rect_dsc->border_width = 2;
            dsc->rect_dsc->border_color = lv_color_hex(0x6495ED);
            lv_draw_rect(dsc->draw_area, dsc->clip_area, dsc->rect_dsc);
            // dsc->label_dsc->align=LV_TEXT_ALIGN_CENTER;
            // lv_draw_label(dsc->draw_area, dsc->clip_area, dsc->label_dsc,lv_table_get_cell_value(obj,irow,icol),NULL);
            //lv_draw_label(&label_area, dsc->clip_area, &label_draw_dsc, buf, NULL);
        lv_draw_rect_dsc_t rect_dsc;
        rect_dsc.bg_color = chk ? lv_theme_get_color_primary(obj) : lv_palette_lighten(LV_PALETTE_GREY, 2);
        rect_dsc.radius = LV_RADIUS_CIRCLE;

        lv_area_t sw_area;
        sw_area.x1 = dsc->draw_area->x2 - 50;
        sw_area.x2 = sw_area.x1 + 40;
        sw_area.y1 =  dsc->draw_area->y1 + lv_area_get_height(dsc->draw_area) / 2 - 10;
        sw_area.y2 = sw_area.y1 + 20;
        lv_draw_rect(&sw_area, dsc->clip_area, &rect_dsc);

        rect_dsc.bg_color = lv_color_white();
        if(chk) {
            sw_area.x2 -= 2;
            sw_area.x1 = sw_area.x2 - 16;
        } else {
            sw_area.x1 += 2;
            sw_area.x2 = sw_area.x1 + 16;
        sw_area.y1 += 2;
        sw_area.y2 -= 2;
        lv_draw_rect(&sw_area, dsc->clip_area, &rect_dsc);

static void change_event_cb(lv_event_t * e)
    lv_obj_t * obj = lv_event_get_target(e);
    uint16_t col;
    uint16_t row;
    lv_table_get_selected_cell(obj, &row, &col);
    bool chk = lv_table_has_cell_ctrl(obj, row, col, LV_TABLE_CELL_CTRL_CUSTOM_1);
    if(chk) lv_table_clear_cell_ctrl(obj, row, col, LV_TABLE_CELL_CTRL_CUSTOM_1);
    else lv_table_add_cell_ctrl(obj, row, col, LV_TABLE_CELL_CTRL_CUSTOM_1);

 * A very light-weighted list created from table
void lv_example_table_2(void)
    /*Measure memory usage*/
    lv_mem_monitor_t mon1;

    uint32_t t = lv_tick_get();

    lv_obj_t * table = lv_table_create(lv_scr_act());

    /*Set a smaller height to the table. It'll make it scrollable*/
    lv_obj_set_size(table, 300, 200);

    lv_table_set_col_width(table, 0, 150);
    lv_table_set_row_cnt(table, ITEM_CNT); /*Not required but avoids a lot of memory reallocation lv_table_set_set_value*/
    lv_table_set_col_cnt(table, 2);

    /*Don't make the cell pressed, we will draw something different in the event*/
    lv_obj_remove_style(table, NULL, LV_PART_ITEMS | LV_STATE_PRESSED);

    uint32_t i;
    for(i = 0; i < ITEM_CNT; i++) {
        lv_table_set_cell_value_fmt(table, i, 0, "Item %d", i + 1);

    //lv_obj_align(table, LV_ALIGN_CENTER, 0, -20);

    /*Add an event callback to to apply some custom drawing*/
    lv_obj_add_event_cb(table, draw_event_cb, LV_EVENT_DRAW_PART_END, NULL);
    lv_obj_add_event_cb(table, change_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_mem_monitor_t mon2;

    uint32_t mem_used = mon1.free_size - mon2.free_size;

    uint32_t elaps = lv_tick_elaps(t);

    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text_fmt(label, "%d items were created in %d ms\n"
                                  "using %d bytes of memory",
                                  ITEM_CNT, elaps, mem_used);

    lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -10);



Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

You might want to track the progress of my issue here:

Its a very similar problem as yours…