OGS
StrictDoubleValidator Class Reference

Detailed Description

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>

Inheritance diagram for StrictDoubleValidator:
[legend]
Collaboration diagram for StrictDoubleValidator:
[legend]

Public Member Functions

 StrictDoubleValidator (double min, double max, std::size_t decimals, QObject *parent=nullptr)
 
 StrictDoubleValidator (QObject *parent=nullptr)
 
QValidator::State validate (QString &input, int &pos) const override
 

Constructor & Destructor Documentation

◆ StrictDoubleValidator() [1/2]

StrictDoubleValidator::StrictDoubleValidator ( double min,
double max,
std::size_t decimals,
QObject * parent = nullptr )
inline

Definition at line 25 of file StrictDoubleValidator.h.

25 :
26 QDoubleValidator( min, max, decimals, parent)
27 {}

◆ StrictDoubleValidator() [2/2]

StrictDoubleValidator::StrictDoubleValidator ( QObject * parent = nullptr)
inlineexplicit

Definition at line 29 of file StrictDoubleValidator.h.

30 : QDoubleValidator(parent)
31 {}

Member Function Documentation

◆ 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: