Hi, Master
Version : LVGL v9.1
Problem :
I want to disable leading zeros in lv_spinbox
. For example, when the spinbox display range is 0.0~999.9
, the integer part has a dynamic number of digits. Currently, using lv_spinbox_set_digit_format(spinbox, 4, 3);
displays unnecessary leading zeros (e.g., 005.6
). I want it to show only the truly significant digits in the integer part (e.g., 5.6
).
Thanks
You can add something like this:
lv_spinbox_add_event_cb( spinbox, spinbox_value_changed_cb, LV_EVENT_VALUE_CHANGED, NULL );
void spinbox_value_changed_cb( lv_event_t* e )
{
lv_obj_t* spinbox = lv_event_get_target_obj( e );
int32_t value = lv_spinbox_get_value( spinbox );
if ( value >= 0 && value <= 9 )
{
lv_spinbox_set_digit_format(spinbox, 2, 1);
return;
}
else if ( value >= 10 && value <= 99 )
{
lv_spinbox_set_digit_format(spinbox, 3, 2);
return;
}
else if ( value >= 100 && value <= 999 )
{
lv_spinbox_set_digit_format(spinbox, 4, 3);
return;
}
}
OK, Thank you very much.
I’ve tried this, and it only partially works for me.
Yes, the leading zero is suppressed, but it also seems to set the spinbox maximum range, but only downwards.E.g.-
lv_spinbox_set_digit_format(spinbox, 1, 0); lv_spinbox_set_value(spinbox, 5);
displays “5” correctly, then followed by -
lv_spinbox_set_digit_format(spinbox, 2, 0); lv_spinbox_set_value(spinbox, 15);
displays “09”
It also seems to upset the center alignment of the number.in the box.
UPDATE
Using this to update the spinbox value avoids the problems I mentioned above -
void LvSetSpinboxVal(lv_obj_t* sb, int v) {
int c = (v < 10) ? 1 : 2;
int max = (v < 10) ? 9 : 99;
lv_spinbox_set_digit_format(sb, c, 0);
lv_spinbox_set_range(sb, 0, max);
lv_spinbox_set_value(sb, v);
}
1 Like