QDoubleValidator 類為浮點數提供範圍校驗。 更多...
| 頭: | #include <QDoubleValidator> |
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 繼承: | QValidator |
| enum | Notation { StandardNotation, ScientificNotation } |
| QDoubleValidator (QObject * parent = nullptr) | |
| QDoubleValidator (double bottom , double top , int decimals , QObject * parent = nullptr) | |
| virtual | ~QDoubleValidator () |
| double | bottom () const |
| int | decimals () const |
| QDoubleValidator::Notation | notation () const |
| void | setBottom (double) |
| void | setDecimals (int) |
| void | setNotation (QDoubleValidator::Notation) |
| void | setRange (double minimum , double maximum , int decimals ) |
| void | setRange (double minimum , double maximum ) |
| void | setTop (double) |
| double | top () const |
| virtual void | fixup (QString & input ) const override |
| virtual QValidator::State | validate (QString & input , int & pos ) const override |
| void | bottomChanged (double bottom ) |
| void | decimalsChanged (int decimals ) |
| void | notationChanged (QDoubleValidator::Notation notation ) |
| void | topChanged (double top ) |
QDoubleValidator provides an upper bound, a lower bound, and a limit on the number of digits after the decimal point.
You can set the acceptable range in one call with setRange (), or with setBottom () 和 setTop (). Set the number of decimal places with setDecimals ()。 validate () function returns the validation state.
QDoubleValidator uses its locale () to interpret the number. For example, in the German locale, "1,234" will be accepted as the fractional number 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits.
注意:
The
QLocale::NumberOptions
設置在
locale
() 還會影響數字的解釋方式。例如,由於
QLocale::RejectGroupSeparator
is not set by default (except on the
"C"
locale), the validator will accept group separators. If the string passes validation, pass it to
locale
().toDouble() to obtain its numeric value.
另請參閱 QIntValidator , QRegularExpressionValidator , QLocale::toDouble (),和 行編輯範例 .
This enum defines the allowed notations for entering a double.
| 常量 | 值 | 描述 |
|---|---|---|
QDoubleValidator::StandardNotation
|
0
|
The string is written in the standard format, a whole number part optionally followed by a separator and fractional part, for example
"0.015"
.
|
QDoubleValidator::ScientificNotation
|
1
|
The string is written in scientific form, which optionally appends an exponent part to the standard format, for example
"1.5E-2"
.
|
The whole number part may, as usual, include a sign. This, along with the separators for fractional part, exponent and any digit-grouping, depend on locale.
QDoubleValidator
doesn't check the placement (which would also depend on locale) of any digit-grouping separators it finds, but it will reject input that contains them if
QLocale::RejectGroupSeparator
有設置在
locale().numberOptions()
.
另請參閱 QLocale::numberOptions (), QLocale::decimalPoint (), QLocale::exponential (),和 QLocale::negativeSign ().
This property holds the validator's minimum acceptable value
By default, this property contains a value of -infinity.
訪問函數:
| double | bottom () const |
| void | setBottom (double) |
通知程序信號:
| void | bottomChanged (double bottom ) |
另請參閱 setRange ().
This property holds the validator's maximum number of digits after the decimal point
By default, this property contains a value of -1, which means any number of digits is accepted.
訪問函數:
| int | decimals () const |
| void | setDecimals (int) |
通知程序信號:
| void | decimalsChanged (int decimals ) |
另請參閱 setRange ().
This property holds the notation of how a string can describe a number
默認情況下,此特性被設為 ScientificNotation .
訪問函數:
| QDoubleValidator::Notation | notation () const |
| void | setNotation (QDoubleValidator::Notation) |
通知程序信號:
| void | notationChanged (QDoubleValidator::Notation notation ) |
另請參閱 Notation .
This property holds the validator's maximum acceptable value
By default, this property contains a value of infinity.
訪問函數:
| double | top () const |
| void | setTop (double) |
通知程序信號:
| void | topChanged (double top ) |
另請參閱 setRange ().
[explicit]
QDoubleValidator::
QDoubleValidator
(
QObject
*
parent
= nullptr)
Constructs a validator object with a parent object that accepts any double.
Constructs a validator object with a parent object. This validator will accept doubles from bottom to top inclusive, with up to decimals digits after the decimal point.
[虛擬]
QDoubleValidator::
~QDoubleValidator
()
銷毀驗證器。
[override virtual, since 6.3]
void
QDoubleValidator::
fixup
(
QString
&
input
) const
重實現: QValidator::fixup(QString &input) const .
這是重載函數。
Attempts to fix the input string to an Acceptable representation of a double.
The format of the number is determined by notation (), decimals (), locale () and the latter's numberOptions ().
To comply with notation (), when ScientificNotation is used, the fixed value will be represented in its normalized form, which means that any non-zero value will have one non-zero digit before the decimal point.
QString input = "0.98765e2"; QDoubleValidator val; val.setLocale(QLocale::C); val.setNotation(QDoubleValidator::ScientificNotation); val.fixup(input); // input == "9.8765e+01"
To comply with
decimals
(), when it is
-1
the number of digits used will be determined by
QLocale::FloatingPointShortest
. Otherwise, the fractional part of the number is truncated (with rounding, as appropriate) if its length exceeds
decimals
(). When
notation
() 是
ScientificNotation
this is done after the number has been put into its normalized form.
input = "-1234.6789"; val.setDecimals(2); val.setLocale(QLocale::C); val.setNotation(QDoubleValidator::StandardNotation); val.fixup(input); // input == "-1234.68"
注意:
若
decimals
() is set to, and the string provides, more than
std::numeric_limits<double>::digits10
, digits beyond that many in the fractional part may be changed. The resulting string shall encode the same floating-point number, when parsed to a
double
.
This function was introduced in Qt 6.3.
Sets the validator to accept doubles from minimum to maximum inclusive, with at most decimals digits after the decimal point.
注意: Setting the number of decimals to -1 effectively sets it to unlimited. This is also the value used by a default-constructed validator.
這是重載函數。
Sets the validator to accept doubles from minimum to maximum inclusive without changing the number of digits after the decimal point.
[override virtual]
QValidator::State
QDoubleValidator::
validate
(
QString
&
input
,
int
&
pos
) const
重實現: QValidator::validate(QString &input, int &pos) const .
返迴 Acceptable 若字符串 input is in the correct format and contains a double within the valid range.
返迴 中間體 if input is in the wrong format or contains a double outside the range.
返迴 Invalid 若 input doesn't represent a double or has too many digits after the decimal point.
Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0) and input is a negative double then Invalid 被返迴。若 notation () 被設為 StandardNotation , and the input contains more digits before the decimal point than a double in the valid range may have, Invalid 被返迴。若 notation () 是 ScientificNotation , and the input is not in the valid range, 中間體 is returned. The value may yet become valid by changing the exponent.
默認情況下, pos 參數並未用於此驗證器。