1234567891011121314151617181920212223242526272829303132333435363738 |
- #include "DoubleValidator.h"
- DoubleValidator::DoubleValidator(double bottom, double top, int decimals, QObject * parent /*= nullptr*/)
- :QDoubleValidator(bottom, top, decimals, parent)
- {
- }
- DoubleValidator::~DoubleValidator()
- {
- }
- DoubleValidator::State DoubleValidator::validate(QString &s, int &i) const
- {
- if (s.isEmpty() || s == "-") {
- return QValidator::Intermediate;
- }
- QChar decimalPoint = locale().decimalPoint();
- if (s.indexOf(decimalPoint) != -1) {
- int charsAfterPoint = s.length() - s.indexOf(decimalPoint) - 1;
- if (charsAfterPoint > decimals()) {
- return QValidator::Invalid;
- }
- }
- bool ok;
- double d = locale().toDouble(s, &ok);
- if (ok && d >= bottom() && d <= top()) {
- return QValidator::Acceptable;
- }
- else {
- return QValidator::Invalid;
- }
- }
|