diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-08-27 18:09:01 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-09-20 09:39:13 +0200 |
commit | b4bb3a5415e44bccee08f3aa5423f217629e158b (patch) | |
tree | 95f3710f54aa987bdde77507bfebb1993f684b05 /src | |
parent | fa9f13f1300cf843aa80786cf4eb217b36aef279 (diff) |
Introduce QDoubleValidator::setRange overload with two parameters
The QDoubleValidator::setRange() used to have 3 parameters, with
the third one (the number of decimals) having a default value of 0.
Such default value does not make much sense for a *double* validator.
Also, since a default value was used, omitting the decimals was
silently overwriting the previous decimals value, discarding the
value that could be previously explicitly specified by user.
[ChangeLog][QtCore][QDoubleValidator][Important Behavior Changes] The
QDoubleValidator::setRange() method now has two overloads.
The first overload takes 3 parameters, but does not support a
default value for decimals.
The second overload takes only two parameters, not changing the
number of decimals at all.
Hence, the number of decimals will only be changed if the user
explicitly specifies it.
To maintain the old behavior of setRange(), pass 0 as the 3rd
argument explicitly.
Note that it is a source-incompatible change. But it should be fine,
because using QDoubleValidator with 0 digits after decimal point does
not make much sense and so, hopefully, is not that common.
At the same time, change the default-constructed QDoubleValidator
to use -1 for decimals, which allows arbitrarily many digits in
the fractional part. The value was previously 1000, which allowed
more than anyone would reasonably use, so this should make no
practical difference.
Some more unit tests to cover the behavior of the setRange()
overloads are also added.
As a dirve-by: remove unnecessary QValidator::State to int conversions
in the unit tests. QCOMPARE is capable of comparing these enums and
provides a better output in case of failure for enums.
Task-number: QTBUG-90719
Change-Id: I523d6086231912e4c07555a89cacd45854136978
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/util/qvalidator.cpp | 21 | ||||
-rw-r--r-- | src/gui/util/qvalidator.h | 3 |
2 files changed, 20 insertions, 4 deletions
diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp index 52dfad11a4..14d9c01503 100644 --- a/src/gui/util/qvalidator.cpp +++ b/src/gui/util/qvalidator.cpp @@ -591,8 +591,8 @@ public: that accepts any double. */ -QDoubleValidator::QDoubleValidator(QObject * parent) - : QDoubleValidator(-HUGE_VAL, HUGE_VAL, 1000, parent) +QDoubleValidator::QDoubleValidator(QObject *parent) + : QDoubleValidator(-HUGE_VAL, HUGE_VAL, -1, parent) { } @@ -801,6 +801,9 @@ void QDoubleValidatorPrivate::fixupWithLocale(QString &input, QLocaleData::Numbe Sets the validator to accept doubles from \a minimum to \a maximum inclusive, with at most \a decimals digits after the decimal point. + + \note Setting the number of decimals to -1 effectively sets it to unlimited. + This is also the value used by a default-constructed validator. */ void QDoubleValidator::setRange(double minimum, double maximum, int decimals) @@ -828,6 +831,17 @@ void QDoubleValidator::setRange(double minimum, double maximum, int decimals) } /*! + \overload + + Sets the validator to accept doubles from \a minimum to \a maximum + inclusive without changing the number of digits after the decimal point. +*/ +void QDoubleValidator::setRange(double minimum, double maximum) +{ + setRange(minimum, maximum, decimals()); +} + +/*! \property QDoubleValidator::bottom \brief the validator's minimum acceptable value @@ -860,7 +874,8 @@ void QDoubleValidator::setTop(double top) \property QDoubleValidator::decimals \brief the validator's maximum number of digits after the decimal point - By default, this property contains a value of 1000. + By default, this property contains a value of -1, which means any number + of digits is accepted. \sa setRange() */ diff --git a/src/gui/util/qvalidator.h b/src/gui/util/qvalidator.h index be9042d653..7728857672 100644 --- a/src/gui/util/qvalidator.h +++ b/src/gui/util/qvalidator.h @@ -142,7 +142,8 @@ public: QValidator::State validate(QString &, int &) const override; void fixup(QString &input) const override; - void setRange(double bottom, double top, int decimals = 0); + void setRange(double bottom, double top, int decimals); + void setRange(double bottom, double top); void setBottom(double); void setTop(double); void setDecimals(int); |