A validator for an input field which only accepts decimals. Source code adapted from StackOverflow
Definition at line 22 of file StrictDoubleValidator.h.
#include <StrictDoubleValidator.h>
◆ StrictDoubleValidator() [1/2]
StrictDoubleValidator::StrictDoubleValidator |
( |
double | min, |
|
|
double | max, |
|
|
std::size_t | decimals, |
|
|
QObject * | parent = nullptr ) |
|
inline |
◆ StrictDoubleValidator() [2/2]
StrictDoubleValidator::StrictDoubleValidator |
( |
QObject * | parent = nullptr | ) |
|
|
inlineexplicit |
◆ validate()
QValidator::State StrictDoubleValidator::validate |
( |
QString & | input, |
|
|
int & | pos ) const |
|
inlineoverride |
Definition at line 33 of file StrictDoubleValidator.h.
34 {
35 Q_UNUSED(pos);
36 if (input.isEmpty() || input == "." || input == "-") return Intermediate;
37
38 QChar const decimalPoint('.');
39 if (input.indexOf(decimalPoint) != -1)
40 {
41 int const charsAfterPoint = input.length() - input.indexOf(decimalPoint) - 1;
42 if (charsAfterPoint > decimals())
43 return QValidator::Invalid;
44 }
45
46 bool ok;
47 double const d = input.toDouble(&ok);
48
49 if (ok && d >= bottom() && d <= top())
50 return QValidator::Acceptable;
51 else
52 return QValidator::Invalid;
53 }
The documentation for this class was generated from the following file: