5

With a QSpinBox is it possible to display the thousand separator of a number while user enter it like 10,000 Which is the best way to do that ?

2 Answers2

5

i know this is late but this may help other people. i used this to update the thousand separator

ui->doubleSpinBox->setGroupSeparatorShown(true);

or

set the property it in the form ui

In my QDialog Form i used this to update the amount with thousand separator,

void DialogCashPayment::on_doubleSpinBox_valueChanged(double arg1){
     ui->doubleSpinBox->setValue(arg1);
}

EDIT:

Found a bug when amount is 10k above, the cursor position is changed. i don't know how to fix this yet. Maybe someone could fix this.

Binsoi
  • 383
  • 5
  • 13
  • Have tested connecting to events on the inherited `lineEdit()` object. Here one can get text-cursor position. Then it is only to compare length of text before and after conversion and re-position the cursor with `setCursorPosition()` – user3342816 Mar 20 '18 at 21:59
2

You could subclass QSpinBox and reimplement the textFromValue which is responsible for the display of the value to the spinbox widget. A possible implementation could be the following:

QString MySpinBox::textFromValue(int value)
{
   return this->locale()->toString(value);
}

Using locale is the best way since it will display the separator based on the user's settings.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
pnezis
  • 12,023
  • 2
  • 38
  • 38
  • it worked , ty , but it do not insert thousand separator when user insert a number with keyboard ! when user focus out than that SpinBox then thousand separator will insert ! how i can fix this ? sory for bad English ! – Sepehr Mohammad Pour Feb 14 '12 at 13:48
  • You could connect the `valueChanged` signal with a custom slot that updates the text. Notice that in this slot you should temproarily block the signals (use `blockSignals(true)`) in order to avoid an endless loop. – pnezis Feb 14 '12 at 16:12
  • How do you actually update the text like that though? – Krenair Feb 22 '17 at 06:31
  • How do i subclass? where do i put that code? i put it in the cpp of my QDialog form and it gave me textFromValue does not match any declaration in 'QDoubleSpinBox' – Binsoi May 24 '17 at 09:06