Use QLineEdit with mask AND validator for money OUTPUT
I created a dialog containing some QLineEdits and a QTableView.
The QLineEdits are connected to a QTableView using QDataWidgetMapper, so, whenever I select a row in QTableView, QLineEdits are filled with their respective values (see below).
One column in the QTableView is CURRENCY type, but it is not showing correctly the value.
For example, the value 1234.50 should be presented in format "1.234,50" (because I'm using Brazilian Real), however, the QLineEdit presents in the format "1234.5", removing the zero and not exchange point by comma.
I'm using setValidator (as specified below), but this works only when I edit the content of the QLineEdit, not when the values are showed.
Please, how can I solve this problem? Thank you for your help everyone.
-- Luciano.
Code:
// Set the mask to Price.
ui->leditPreco->setInputMask("000.009,99;_");
// Set validator to Price.
QRegExp preco
("^\\d{1,3}(([.]\\d{3})*),(\\d{2})$");
...
_model = new TSqlTableModel(this); _model->setTable("Produtos");
...
_model
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Price"));
...
ui->tviewProdutos->setModel(_model);
_mapper->addMapping(ui->leditCodigo, 0);
_mapper->addMapping(ui->leditDescr, 1);
_mapper->addMapping(ui->leditQtde, 2);
_mapper->addMapping(ui->leditPreco, 3);
// Populate the table and go to first record.
_model->select();
_mapper->toFirst();
connect( ui->tviewProdutos->selectionModel()
, _mapper
);
Re: Use QLineEdit with mask AND validator for money OUTPUT
The input mask and the validator don't go together well. It's best to implement the mask using the validator API.
Re: Use QLineEdit with mask AND validator for money OUTPUT
Quote:
Originally Posted by
wysota
The input mask and the validator don't go together well. It's best to implement the mask using the validator API.
Yes, I did this, as you can see in the source code, but still does not work:
Code:
//removed ui->leditPreco->setInputMask("000.009,99;");
QRegExp preco
("^(\\d{1,3}(\\.\\d{3})*|(\\d+))(\\,\\d{2})?$");
Re: Use QLineEdit with mask AND validator for money OUTPUT
No, you didn't. You used both the validator and the input mask. You should subclass QValidator and implement proper validation for your data including the fixup routine.
Re: Use QLineEdit with mask AND validator for money OUTPUT
Quote:
Originally Posted by
wysota
No, you didn't. You used both the validator and the input mask. You should subclass
QValidator and implement proper validation for your data including the fixup routine.
Yes. You're right. I subclass and it works. Thank you, wysota!