I'm hoping to get some insight into a issue I'm facing using QDoubleValidator, I have created a widget to collect some information, and would like to have some fields validated as Double values, I have done so here:
orderform::orderform(QWidget *parent) :
QDialog(parent),
ui(new Ui::orderform)
{
ui->setupUi(this);
this->setWindowTitle("Order Entry / Edt");
ui->edtOrderDate->setDate(QDate::currentDate()); //Sets date box to current date
//----- Validator for float input in 'money' fields -----//
auto dv = new QDoubleValidator(0.0,5.0,2);
ui->edtSoldFor->setValidator(dv);
ui->edtItemSubtotal->setValidator(dv);
ui->edtShipping->setValidator(dv);
ui->edtSalesTaxPFC->setValidator(dv);
ui->edtDiscount->setValidator(dv);
ui->edtOrderTotal->setValidator(dv);
ui->edtTotalFee->setValidator(dv);
ui->edtVAT->setValidator(dv);
ui->edtFeeIncVAT->setValidator(dv);
ui->edtBoughtFor->setValidator(dv);
ui->edtSupSubTotal->setValidator(dv);
ui->edtSupShipping->setValidator(dv);
ui->edtSupTax->setValidator(dv);
ui->edtSupOrderTotal->setValidator(dv);
ui->edtAddExpense->setValidator(dv);
ui->edtAddDiscount->setValidator(dv);
//----- -----//
}
in addition each of these fields has a slot assigned to them to run a calculation when editing the field finishes, here is an example:
void orderform::on_edtSoldFor_editingFinished()
{
calcPltfrmOrderTotals();
}
and here is the function it calls:
void orderform::calcPltfrmOrderTotals() //This function is used to calculate all totals
{
//Var:
double valueAsDouble = 0;
QString valueAsString;
//Calc Order Sub Total
valueAsDouble = (ui->edtBoughtFor->text().toDouble() * ui->sbSupQty->value());
valueAsString = QString::number(valueAsDouble);
ui->edtSupSubTotal->setText(valueAsString);
//Calc Supplier Sub Total
valueAsDouble = (ui->edtSoldFor->text().toDouble() * ui->sbItemQty->value());
valueAsString = QString::number(valueAsDouble);
ui->edtItemSubtotal->setText(valueAsString);
//Calc Order Total on platform
valueAsDouble = ((ui->edtItemSubtotal->text().toDouble() ui->edtShipping->text().toDouble() ui->edtSalesTaxPFC->text().toDouble()) - ui->edtDiscount->text().toDouble());
valueAsString = QString::number(valueAsDouble);
ui->edtOrderTotal->setText(valueAsString);
//Calc fee total on platform
valueAsDouble = (ui->edtTotalFee->text().toDouble() ui->edtVAT->text().toDouble());
valueAsString = QString::number(valueAsDouble);
ui->edtFeeIncVAT->setText(valueAsString);
//Calc Supplier Order Total
valueAsDouble = (ui->edtSupSubTotal->text().toDouble() ui->edtSupShipping->text().toDouble() ui->edtSupTax->text().toDouble());
valueAsString = QString::number(valueAsDouble);
ui->edtSupOrderTotal->setText(valueAsString);
//Calc PNL
valueAsDouble = (((ui->edtItemSubtotal->text().toDouble() - ui->edtFeeIncVAT->text().toDouble()) - ui->edtSupOrderTotal->text().toDouble())
- ui->edtAddExpense->text().toDouble()) ui->edtAddDiscount->text().toDouble();
valueAsString = QString::number(valueAsDouble);
ui->lcdPNL->display(valueAsDouble);
};
the problem is before I added the validation to the fields, the calculations would run correctly and instantly after the edit finishes, but as soon as i add the validation the calculations on 'editfinish' is very hit and miss, sometime it works and other times it doesn't and i have no idea why.
CodePudding user response:
Maybe your validator range (0.0 to 5.0) is too narrow. If a newly calculated value falls outside the range, the validator state won't be QValidator::Acceptable anymore, and, as a side effect, the line edit will no longer emit the editingFinished signal.
You could try keeping the validator's top() to its default (infinity) value, maybe using the other constructor and using setters to set decimals and range bottom:
auto dv = new QDoubleValidator();
dv->setDecimals(2);
dv->setBottom(0.0);
