OGS
StrictDoubleValidator.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <QDoubleValidator>
17
22class StrictDoubleValidator : public QDoubleValidator
23{
24public:
25 StrictDoubleValidator ( double min, double max, std::size_t decimals, QObject* parent = nullptr) :
26 QDoubleValidator( min, max, decimals, parent)
27 {}
28
29 explicit StrictDoubleValidator(QObject* parent = nullptr)
30 : QDoubleValidator(parent)
31 {}
32
33 QValidator::State validate(QString& input, int& pos) const override
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 }
54};
A validator for an input field which only accepts decimals. Source code adapted from StackOverflow
StrictDoubleValidator(QObject *parent=nullptr)
QValidator::State validate(QString &input, int &pos) const override
StrictDoubleValidator(double min, double max, std::size_t decimals, QObject *parent=nullptr)