Migrating from 7.5 to 8.1 - Table cell align (lv_table_set_cell_align)

Description

In 7.5 i used lv_table_set_cell_align to set the alignment of text in a cell. This doesnt exist in 8.1. What do we use now?

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

Simulator

What LVGL version are you using?

8.1

What do you want to achieve?

Align the content of a table cell right.

What have you tried so far?

Review docs

Code to reproduce

7.5 used to be

lv_table_set_cell_align(sensorTable, i, 1, LV_TEXT_ALIGN_RIGHT);

Screenshot and/or video

1 Like

See here: Table (lv_table) β€” LVGL documentation

Unfortunately that didnt work, the table still looks the same.
Could it be because it’s being updated via a timer?

    sensorTable = lv_table_create(parent);

    lv_obj_align_to(sensorTable, parent, LV_ALIGN_TOP_LEFT, 30, 25);

    lv_obj_set_style_radius(sensorTable, 10, 0);
    lv_obj_set_style_clip_corner(sensorTable, true, 0);

    lv_obj_set_style_pad_top(sensorTable, 2, LV_PART_MAIN);
    lv_obj_set_style_pad_bottom(sensorTable, 2, LV_PART_MAIN);
    lv_obj_set_style_pad_left(sensorTable, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_right(sensorTable, 0, LV_PART_MAIN);

    lv_table_set_col_cnt(sensorTable, 4);
    lv_table_set_row_cnt(sensorTable, sizeof(screenTemps)/sizeof(struct screenTemp));

    lv_table_set_col_width(sensorTable, 0, 175);
    lv_table_set_col_width(sensorTable, 1, 110);
    lv_table_set_col_width(sensorTable, 2, 65);
    lv_table_set_col_width(sensorTable, 3, 75);

    printf("Merge Cells\n");
    lv_table_add_cell_ctrl(sensorTable, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
    lv_table_add_cell_ctrl(sensorTable, 0, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
    lv_table_add_cell_ctrl(sensorTable, 0, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);

    /*Header Row*/
    lv_table_set_cell_value(sensorTable, 0, 0, "Temps");

    //8.1 to fix
    //lv_table_set_cell_align(sensorTable, 0, 0, LV_TEXT_ALIGN_CENTER);

    /*Row 1*/
    for (int i = 1; i <= sizeof(screenTemps)/sizeof(struct screenTemp)+1; i++) {
  	  /*
	  lv_obj_set_style__pad_top(sensorTable, i-1, LV_STATE_DEFAULT, 2);
	  lv_obj_set_style__pad_bottom(sensorTable, i-1, LV_STATE_DEFAULT, 2);
	  */
      lv_table_set_cell_value(sensorTable, i, 0, "");
      lv_table_set_cell_value(sensorTable, i, 1, "");
      //8.1 to fix
      //lv_table_set_cell_align(sensorTable, i, 1, LV_TEXT_ALIGN_RIGHT);
      lv_table_set_cell_value(sensorTable, i, 2, "");
      //8.1 to fix
      //lv_table_set_cell_align(sensorTable, i, 2, LV_TEXT_ALIGN_RIGHT);
      lv_table_set_cell_value(sensorTable, i, 3, "");
    }

    #define pad 8

    printf("Set Cell Padding\n");
    lv_obj_set_style_pad_top(sensorTable, pad, LV_PART_ITEMS);
    lv_obj_set_style_pad_bottom(sensorTable, pad, LV_PART_ITEMS);
    lv_obj_set_style_pad_left(sensorTable, 2, LV_PART_ITEMS);
    lv_obj_set_style_pad_right(sensorTable, 0, LV_PART_ITEMS);

    lv_obj_add_event_cb(sensorTable, draw_part_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);

static void draw_part_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_param(e);
    /*If the cells are drawn...*/
    if(dsc->part == LV_PART_ITEMS) {
        uint32_t row = dsc->id /  lv_table_get_col_cnt(obj);
        uint32_t col = dsc->id - row * lv_table_get_col_cnt(obj);

        /*In the first column align the texts to the right*/
        if(col == 1 || col == 2) {
            dsc->label_dsc->flag = LV_TEXT_ALIGN_RIGHT;
        }

    }
}

Tasks.c:

void updateScreen()
{

  for (int i = 1; i <= (sizeof(screenTemps)/sizeof(struct screenTemp))+1; i++)
  {

    printTempToScreenLastSecOnly(i);

  }

}

void printTempToScreenLastSecOnly(int arraypos)
{

  int lastMessage = screenTemps[arraypos].lastLCDMsg;
  if(lastMessage > 0)
  {
	  int secsSinceLastMessage = (millis() /*/ 1000*/) - lastMessage;

	  char buf[256];

	  sprintf(buf, "%d", secsSinceLastMessage);

	  lv_table_set_cell_value(sensorTable, arraypos, 2, buf);
  }

}

Result:
image

Oh, sorry, there was a typo in the example.

align should be used instead of flag:

dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;

Bingo!
Thats it!
image

Thank you :smiley:

1 Like