A
A
Artem Markovich2014-02-06 09:50:32
Qt
Artem Markovich, 2014-02-06 09:50:32

QLineedit: how to split a number into digits when typing (1500000) to (1,500,000)?

As an example of a calculator in Windows 7, when entering a number, it automatically breaks into digits [8 696 000] how to implement this in Qt? QLineedit has a setInputMask method but it allows you to enter numbers of a fixed length (999 999) it will allow you to enter 6 characters no more, no less ... The Validator checks the input for compliance, but does not change the input format ... There is also a QLocale that seems to do what needed ... but it leads to the desired format of an already entered value ...
So for label it fits

connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(FormatText()));
void Dialog::FormatText()
{
    ui->label_2->setText(QString("%L1").arg(ui->lineEdit->text().toDouble(),0,'f',0,' '));
}

But I would like to format in QLineedit itself...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tugo, 2014-02-06
@Gizmich

Create your own class, inherit from QLineEdit and override some methods to implement your task.
For example, if the widget is read only and you use the insert method, override it, validate the incoming text and add spaces where necessary, then call insert on the base class.
If the user enters the numbers himself, you need to catch the appearance of a new character (or deletion), read the text, add or remove spaces.

D
dkoryagin, 2014-02-06
@dkoryagin

While the option comes to mind to catch the keypress event and insert spaces every 3 characters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question