DoubleValidator.cpp 782 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "DoubleValidator.h"
  2. DoubleValidator::DoubleValidator(double bottom, double top, int decimals, QObject * parent /*= nullptr*/)
  3. :QDoubleValidator(bottom, top, decimals, parent)
  4. {
  5. }
  6. DoubleValidator::~DoubleValidator()
  7. {
  8. }
  9. DoubleValidator::State DoubleValidator::validate(QString &s, int &i) const
  10. {
  11. if (s.isEmpty() || s == "-") {
  12. return QValidator::Intermediate;
  13. }
  14. QChar decimalPoint = locale().decimalPoint();
  15. if (s.indexOf(decimalPoint) != -1) {
  16. int charsAfterPoint = s.length() - s.indexOf(decimalPoint) - 1;
  17. if (charsAfterPoint > decimals()) {
  18. return QValidator::Invalid;
  19. }
  20. }
  21. bool ok;
  22. double d = locale().toDouble(s, &ok);
  23. if (ok && d >= bottom() && d <= top()) {
  24. return QValidator::Acceptable;
  25. }
  26. else {
  27. return QValidator::Invalid;
  28. }
  29. }