0

I was wondering either there is a way to set the QLcdNumber widget to have a fixed format width. For example, I would like to set the widget to always display 3 numbers before coma and 2 after:

000.00
001.00
120.50
100.25
etc.

Is there a way to do this? I would apreciate all help.

Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

2 Answers2

2

May be not as easy, as you would like, but this works:

lcdNumber->setDigitCount(6);

...

double d = 1.2;
int i = d;
lcdNumber->display(QString("%1").arg(i, 3, 10, QChar('0'))
                   + "."
                   + QString("%1").arg(qRound((d - i) * 100), 2, 10, QChar('0')));
Amartel
  • 4,248
  • 2
  • 15
  • 21
0

Some findings and behaviours I made in the area.

If the smalDecimalPoint set to False, the decimal point count as a digit. If you feed the display() with a double, trailing '0' decimals will be truncated (see code example below).

The decimal behaviour of the QLcdNumber is a bit annoying if the widget takes a large double (== many digits in play) and update rapidly. For smal numbers there is a large number of decimals and the number of digits shown depend on the value is relatively even or not (e.g. 1,500000 or 1,51231231)

double d = 1.2300000;
lcdNumber->setDigitCount(6);
lcdNumber->display(d);
// Result 1.23 -- not 1.2300 as wanted/expected...

d = 12.3456789;
lcdNumber->display(d);
// Result 12.345  -- six digits including decimal point

lcdNumber->smalDecimalPoint = false;
lcdNumber->display(d);
// Result 12.3456 -- six digits excluding decimal point

So for a smooth visual not jumpy LCDnumber, the solution that Amartel presented are the only way...

// well, there is always an other way
// Old school
double d = 1.23456789
char buffer[7];
sprintf(buffer, "%03.02f", d);
lcdNumber->display(QString(buffer));
// Have not test this, but should work :-/, but that is a bit c-old-style-not-so-safe :-)
// might need to cast the buffer to be accepted for QString
Mats
  • 33
  • 6