Good day! I implement the calculator on Qt. The following questions arose:

  1. I used QString::number(a1, 'f', 10) for output not in exponential form, but in the end I get extra zeros at the end. Can QDoubleValidator be used to get rid of zeros?
  2. Need control input comma in the number (you can enter only one comma). I think it can be implemented like this:

     QString str; int i; QLineEdit *Result = new QLineEdit(this); if (Result->validator()->validate(str,i)==QValidator::Invalid) return; 

    Maybe there is a better way or is it not at all correct?

Thanks for the help and the time spent.

  • And after QString::number(a1, 'f', 10) do you get a string with a dot or comma? - cassini
  • @cassini with a comma string - Dexter Morgan

2 answers 2

Perhaps QDoubleValidator not useful here.

  1. When tailing a QString from a double tail zeroes can be truncated as follows:

     QString str = QString::number(a1, 'f', 10); str.remove(QRegExp(",?0+$")); 

    If there is nothing after the comma, it will be cut off.

  2. You can use QRegExpValidator to control input:

     QRegExpValidator *v = new QRegExpValidator(QRegExp("^\\d+|\\d+,\\d+$")); lineEdit->setValidator(v); 

    In this form, it will not allow you to enter more than one comma and enter a comma as the first character, but allows you to leave a comma at the end. Calling validate() on this validator for a string with a comma at the end will return QValidator::Intermediate .

    QDoubleValidator is quite possible to use QDoubleValidator here, since it can be given its own templates, with the number of zeros at the end - Doc by QDoubleValidator

    • But this will not help? doubleValidator->setNotation(QDoubleValidator::StandardNotation); - Dexter Morgan
    • @ Dexter Morgan yes, I checked, can help. Try it anyway. - vadrozh
    • do not show how checked? I have this output line ui->display->setText(text + digit); - Dexter Morgan
    • The answer above is essentially what I suggested before correcting this answer. - vadrozh